<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>HongmeiG</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/hongmeig1/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/hongmeig1/feed.xml</link>
    <description />
    <lastBuildDate>Sun, 12 Apr 2015 19:37:31 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>WCF: NotSupportedException: The SSL settings for the service 'SslRequireCert' does not match those of the IIS 'Ssl'.</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/wcf-notsupportedexception-the-ssl-settings-for-the-service-sslrequirecert-does-not-match-those-of-the-iis-ssl</link>
      <pubDate>Thu, 23 May 2013 22:33:00 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2013/05/23/wcf-notsupportedexception-the-ssl-settings-for-the-service-sslrequirecert-does-not-match-those-of-the-iis-ssl/</guid>
      <description><![CDATA[When you are using WCF with client certificate over SSL in web hosted case, you might hit an...]]></description>
      <content:encoded><![CDATA[<p>When you are using WCF with client certificate over SSL in web hosted case, you might hit an exception like the following:</p>
<p>[NotSupportedException: The SSL settings for the service 'SslRequireCert' does not match those of the IIS 'Ssl'.]</p>
<p>System.ServiceModel.Activation.HostedAspNetEnvironment.ValidateHttpsSettings(String virtualPath, Nullable`1&amp; requireClientCertificate) +117347 System.ServiceModel.Channels.HttpsChannelListener.ApplyHostedContext(String virtualPath, Boolean isMetadataListener) +97 System.ServiceModel.Activation.HostedAspNetEnvironment.ApplyHostedContext(TransportChannelListener listener, BindingContext context) +84 System.ServiceModel.Channels.HttpsTransportBindingElement.BuildChannelListener(BindingContext context) +93 System.ServiceModel.Channels.BindingContext.BuildInnerChannelListener() +63 System.ServiceModel.Channels.MessageEncodingBindingElement.InternalBuildChannelListener(BindingContext context) +67</p>
<p>&hellip;</p>
<h3>Why?</h3>
<p>The exception text is actually quite informative in this case. The IIS configuration is telling you that it is only SSL with any cert but the WCF is configured with client certificate over SSL. Your configuration section could look like the following.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;security mode="Transport"&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;transport clientCredentialType="Certificate" /&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/security&gt;</p>
<p>&nbsp;</p>
<h3>How to fix that?</h3>
<p>Now go to your inetmgr, navigate to the virtual directory, and click on the SSL Setting, and make sure &ldquo;Require&rdquo; certificate is selected instead of &ldquo;Ignore&rdquo;. Now, if you have a sub directory under the v-dir, make sure you click on any sub directory as well and double check your SSL setting to require certificate.</p>
<p>Alternatively, open the master iis Host config file at C:\Windows\System32\inetsrv\config\applicationHost.config, and then search for &ldquo;Default Web Site/MyVdir&rdquo;. And see if it only contains &ldquo;ssl&rdquo;, like the following:</p>
<p>&nbsp;&nbsp; &lt;location path="Default Web Site/MyVdir"&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;system.webServer&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;security&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;access sslFlags="<strong>Ssl</strong>" /&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/security&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/system.webServer&gt; <br />&nbsp;&nbsp;&nbsp; &lt;/location&gt;</p>
<p>To fix this, just add additional sslFlags to look like the following.</p>
<p>&nbsp; &lt;location path="Default Web Site/MyVdir"&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;system.webServer&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;security&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;access sslFlags="<strong>Ssl, SslNegotiateCert, SslRequireCert</strong>" /&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/security&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/system.webServer&gt; <br />&nbsp;&nbsp;&nbsp; &lt;/location&gt;</p>
<p>Now make sure the changes are saved and do an iisreset before trying to hit the server again.</p>]]></content:encoded>
    </item>
    <item>
      <title>Learning SignalR: Unable to get property 'client' of undefined or null reference</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/learning-signalr-unable-to-get-property-client-of-undefined-or-null-reference</link>
      <pubDate>Thu, 18 Apr 2013 11:52:00 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2013/04/18/learning-signalr-unable-to-get-property-client-of-undefined-or-null-reference/</guid>
      <description><![CDATA[One of the most common exceptions you will hit when you first started with a SignalR chat program...]]></description>
      <content:encoded><![CDATA[<p>One of the most common exceptions you will hit when you first started with a SignalR chat program could look like the following: </p>  <p>Unhandled exception at line 78, column 13 in <a href="https://localhost:53632/home/chat">https://localhost:53632/home/chat</a></p>  <p>0x800a138f - JavaScript runtime error: Unable to get property 'client' of undefined or null reference </p>  <p><strong>Why?</strong> </p>  <p>This is mostly like caused by the fact that your Hub class name and your javascript probably does not match. <strong>It is case sensitive</strong>. By default, SignalR runtime uses <strong>camel casing</strong> of the Hub class name.</p>  <p>For example, if your hub class looks like the following:&#160; </p>  <div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">   <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">     <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyChatHub : Hub </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060">   2:</span> { </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Send(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> message) </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060">   4:</span>     { </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060">   5:</span>         Clients.All.broadcastMyMessage(name, message); </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060">   6:</span>     } </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060">   7:</span> }</pre>
<!--CRLF--></div>
</div>

<p>And the proxy referenced in your HtmlPage1.html might look like the following:</p>

<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">
  <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #008000">// Declare a proxy to reference the hub.  </span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">var</span> chat = $.connection.MyChatHub;</pre>
<!--CRLF--></div>
</div>

<p> Now this does not work since <strong>camel casing</strong> means that you need to use &quot;myChatHub&quot; instead. so to fix it, you can modify your client side page to use the correct casing. </p>

<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">
  <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #008000">// Declare a proxy to reference the hub.  </span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">var</span> chat = $.connection.myChatHub;</pre>
<!--CRLF--></div>
</div>

<p>Now if you would like to override this default behavior, you could add the HubName attribute to your MyChatHub class. SignalR runtime will use your name as it is, in whatever casing you like, as the name of the proxy. </p>

<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">
  <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060">   1:</span> [HubName(<span style="color: #006080">&quot;MyChatHub&quot;</span>)] </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyChatHub : Hub </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060">   3:</span> { </pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #008000">// omit the method here for simplicity </span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: &#39;Courier New&#39;, courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060">   5:</span> }</pre>
<!--CRLF--></div>
</div>

<p>&#160;</p>

<p>Hope this helps.</p>]]></content:encoded>
    </item>
    <item>
      <title>How to customize parameter binding</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-customize-parameter-binding</link>
      <pubDate>Fri, 28 Sep 2012 11:47:00 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/09/28/how-to-customize-parameter-binding/</guid>
      <description><![CDATA[Web API provides some great out of box experience with parameter binding. It also provides some...]]></description>
      <content:encoded><![CDATA[<p>Web API provides some great out of box experience with parameter binding. It also provides some powerful extensibility points&nbsp;to allow&nbsp;you&nbsp;customize that binding process. For example, you can bind multiple parameters from uri. However, it can only bind one parameter from&nbsp;request content by default. If you try to mark multiple parameters&nbsp;with [FromBody] attribute, you&nbsp;will get an error. The other good&nbsp;example&nbsp;is sometimes people might want to&nbsp;bind a parameter from some other source instead of request. There are also occasions where some parameter might come from either request uri or request body. For all those situations,&nbsp; a custom parameter binding will be your friend.</p>
<h3>Scenario 1: How to bind a parameter not coming from request</h3>
<p>Say you have a controller that will take an IPrincipal as an parameter. The IPrincipal is not coming from request. So your action looks like the following.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="12">
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">HttpGet</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> BindPrincipal(<span style="color: #2b91af;">IPrincipal</span> principal)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> Request.CreateResponse(System.Net.<span style="color: #2b91af;">HttpStatusCode</span>.Accepted,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">String</span>.Format(<span style="color: #a31515;">"BindPrincipal with Principal name {0}."</span>, principal.Identity.Name));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p>&nbsp;Here is your custom parameter binding to bind&nbsp;a custom principal to the IPrincipal parameter.&nbsp;</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="9">
<li><span style="color: #808080;">&nbsp;&nbsp;&nbsp; ///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> A Custom HttpParameterBinding to bind a complex type, e.g. IPrincipal, which is not coming from request</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">PrincipalParameterBinding</span> : <span style="color: #2b91af;">HttpParameterBinding</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> PrincipalParameterBinding(<span style="color: #2b91af;">HttpParameterDescriptor</span> desc)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>(desc)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">Task</span> ExecuteBindingAsync(<span style="color: #2b91af;">ModelMetadataProvider</span> metadataProvider, <span style="color: #2b91af;">HttpActionContext</span> actionContext, <span style="color: #2b91af;">CancellationToken</span> cancellationToken)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// Set the binding result here</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetValue(actionContext, <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">GenericPrincipal</span>(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">GenericIdentity</span>(<span style="color: #a31515;">"MyIdentity"</span>), <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">string</span>[]{<span style="color: #a31515;">"myrole"}</span>));</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// now, we can return a completed task with no result</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">TaskCompletionSource</span>&lt;<span style="color: #2b91af;">AsyncVoid</span>&gt; tcs = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">TaskCompletionSource</span>&lt;<span style="color: #2b91af;">AsyncVoid</span>&gt;();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tcs.SetResult(<span style="color: #0000ff;">default</span>(<span style="color: #2b91af;">AsyncVoid</span>));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> tcs.Task;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">struct</span> <span style="color: #2b91af;">AsyncVoid</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p>&nbsp;</p>
<h3>Scenario 2:&nbsp;&nbsp;How to bind a complex type either from body or from uri</h3>
<p>&nbsp;Sometimes, server might need to handle different type of requests. Some requests&nbsp;have information stored in the request Uri, and others have&nbsp;them stored in the request body. Here is how you can write one action to handle both types of requests. Your action code&nbsp;might look like the following:</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="19">
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">HttpGet</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">HttpPost</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> BindCustomComplexTypeFromUriOrBody(<span style="color: #2b91af;">TestItem</span> item)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> Request.CreateResponse(System.Net.<span style="color: #2b91af;">HttpStatusCode</span>.Accepted,</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">String</span>.Format(<span style="color: #a31515;">"BindCustomComplexTypeFromUriOrBody returns item.Name = {0}."</span>, item.Name));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p><span style="color: #008000; font-family: Consolas; font-size: x-small;" face="Consolas" color="#008000" size="2"><span style="color: #008000; font-family: Consolas; font-size: x-small;" face="Consolas" color="#008000" size="2"><span style="color: #008000; font-family: Consolas; font-size: x-small;" face="Consolas" color="#008000" size="2"></span></span></span>&nbsp;Your&nbsp;custom parameter&nbsp;binding can delegate the work to either FromUri or FromBody.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="9">
<li><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li style="background: #f3f3f3;"><span style="color: #808080;">///</span><span style="color: #008000;"> A Custom HttpParameterBinding to bind a complex type either from request body or uri</span></li>
<li><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">FromUriOrBodyParameterBinding</span> : <span style="color: #2b91af;">HttpParameterBinding</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpParameterBinding</span> _defaultUriBinding;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpParameterBinding</span> _defaultFormatterBinding;</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> FromUriOrBodyParameterBinding(<span style="color: #2b91af;">HttpParameterDescriptor</span> desc)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>(desc)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_defaultUriBinding = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">FromUriAttribute</span>().GetBinding(desc);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_defaultFormatterBinding = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">FromBodyAttribute</span>().GetBinding(desc);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">Task</span> ExecuteBindingAsync(<span style="color: #2b91af;">ModelMetadataProvider</span> metadataProvider, <span style="color: #2b91af;">HttpActionContext</span> actionContext, <span style="color: #2b91af;">CancellationToken</span> cancellationToken)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (actionContext.Request.Content != <span style="color: #0000ff;">null</span> &amp;&amp; actionContext.Request.Content.Headers.ContentLength &gt; 0)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// we have something from the body, try that first</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> _defaultFormatterBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// we need to read things from uri</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> _defaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">}</li>
</ol></div>
</div>
<p>&nbsp;</p>
<h3>Scenario 3: How to bind a parameter with a different property Name</h3>
<p>Due to various reasons, the request sometimes contains a different name for a property than the actual property name defined on the server. How do we get those mapped correctly? For example, here is the type definition that its property is named "Name" on the server, but the client wants to send it&nbsp;&nbsp;using "$Name" instead.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="5">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">TestItemRenameProperty</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Name = <span style="color: #a31515;">"$Name"</span>)]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Name { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }</li>
<li>}</li>
</ol></div>
</div>
<p>Here is how the action could look like.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="27">
<li>[<span style="color: #2b91af;">HttpGet</span>]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> BindCustomComplexTypeFromUriWithRenamedProperty(<span style="color: #2b91af;">TestItemRenameProperty</span> item)</li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> Request.CreateResponse(System.Net.<span style="color: #2b91af;">HttpStatusCode</span>.Accepted,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">String</span>.Format(<span style="color: #a31515;">"BindCustomComplexTypeFromUriWithRenamedProperty item.Name = {0}."</span>, item.Name));</li>
<li style="background: #f3f3f3;">}</li>
</ol></div>
</div>
<h3><br />Scenario 4: How to bind multiple parameters form a request body</h3>
<p>This might be the most common case where you want to use custom parameter bindings. Out of box, one could only bind one parameter from request body. It forces&nbsp;user who want to bind multiple objects having to write a wrapper class to wrap all those parameters. Fortunately, you can&nbsp;also&nbsp;write a custom parameter binding to solve that problem elegantly. Here is how your action might look like, and your first name and last name&nbsp;are coming from request body. &nbsp;</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="34">
<li>[<span style="color: #2b91af;">HttpPost</span>]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> PostMultipleParametersFromBody(<span style="color: #0000ff;">string</span> firstname, <span style="color: #0000ff;">string</span> lastname)</li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> Request.CreateResponse(System.Net.<span style="color: #2b91af;">HttpStatusCode</span>.Accepted,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">String</span>.Format(<span style="color: #a31515;">"BindCustomCoPostMultipleParametersFromBodymplexType FristName = {0}, LastName = {1}."</span>, firstname, lastname));</li>
<li style="background: #f3f3f3;">}</li>
</ol></div>
</div>
<p>Now let us&nbsp;write a parameter binding to map those values from request body to firstname and lastname parameters.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="11">
<li><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li style="background: #f3f3f3;"><span style="color: #808080;">///</span><span style="color: #008000;"> A Custom HttpParameterBinding to bind multiple parameters from request body</span></li>
<li><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MultipleParameterFromBodyParameterBinding</span> : <span style="color: #2b91af;">HttpParameterBinding</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> MultipleBodyParameters = <span style="color: #a31515;">"MultipleBodyParameters"</span>;</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> MultipleParameterFromBodyParameterBinding(<span style="color: #2b91af;">HttpParameterDescriptor</span> descriptor)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>(descriptor)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">Task</span> ExecuteBindingAsync(<span style="color: #2b91af;">ModelMetadataProvider</span> metadataProvider, <span style="color: #2b91af;">HttpActionContext</span> actionContext, <span style="color: #2b91af;">CancellationToken</span> cancellationToken)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">NameValueCollection</span> col = TryReadBody(actionContext.Request);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (col == <span style="color: #0000ff;">null</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpResponseException</span>(System.Net.<span style="color: #2b91af;">HttpStatusCode</span>.BadRequest);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// Set the binding result here</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetValue(actionContext, col[Descriptor.ParameterName]);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// now, we can return a completed task with no result</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">TaskCompletionSource</span>&lt;<span style="color: #2b91af;">AsyncVoid</span>&gt; tcs = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">TaskCompletionSource</span>&lt;<span style="color: #2b91af;">AsyncVoid</span>&gt;();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tcs.SetResult(<span style="color: #0000ff;">default</span>(<span style="color: #2b91af;">AsyncVoid</span>));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> tcs.Task;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">NameValueCollection</span> TryReadBody(<span style="color: #2b91af;">HttpRequestMessage</span> request)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">object</span> result = <span style="color: #0000ff;">null</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (!request.Properties.TryGetValue(MultipleBodyParameters, <span style="color: #0000ff;">out</span> result))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// parsing the string like firstname=Hongmei&amp;lastname=Ge</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">NameValueCollection</span> collection = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NameValueCollection</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = request.Content.ReadAsFormDataAsync().Result;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.Properties.Add(MultipleBodyParameters, result);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> result <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">NameValueCollection</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">struct</span> <span style="color: #2b91af;">AsyncVoid</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">}</li>
</ol></div>
</div>
<p>&nbsp;</p>
<h3>Final step: How to register a custom parameter binding</h3>
<p>Now we have all those wonderful custom Parameter Bindings, how do you&nbsp;tell Web API that you want to use them instead of those default ones? Here is how you can do it.&nbsp;</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="29">
<li><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Register an action to create custom ParameterBinding</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.ParameterBindingRules.Insert(0, GetCustomParameterBinding);</li>
</ol></div>
</div>
<p>&nbsp;</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 3em; padding: 0px 0px 0px 5px;" start="102">
<li><span style="color: #0000ff;">&nbsp;&nbsp;&nbsp;&nbsp; public</span> <span style="color: #0000ff;">static</span> <span style="color: #2b91af;">HttpParameterBinding</span> GetCustomParameterBinding(<span style="color: #2b91af;">HttpParameterDescriptor</span> descriptor)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (descriptor.ParameterType == <span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">IPrincipal</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">PrincipalParameterBinding</span>(descriptor);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ( descriptor.ParameterType == <span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">TestItem</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">FromUriOrBodyParameterBinding</span>(descriptor);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (descriptor.ParameterType == <span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">TestItemRenameProperty</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">CustomModelBinderWithPropertyName</span>(descriptor);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> ( descriptor.ParameterType == <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">string</span>) )</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MultipleParameterFromBodyParameterBinding</span>(descriptor);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// any other types, let the default parameter binding handle</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p>&nbsp;</p>
<p>To summarize, custom parameter binding is a powerful extensibility point which allows users to&nbsp;take full&nbsp;control of the&nbsp;parameter binding process. To learn more details,&nbsp;you can&nbsp;download the sample code from our aspnet codeplex site at <a href="https://aspnet.codeplex.com/SourceControl/changeset/66590b705e5b">https://aspnet.codeplex.com/SourceControl/changeset/66590b705e5b.</a></p>
<p>Happy coding with ASPNET Web API!</p>]]></content:encoded>
    </item>
    <item>
      <title>Great Hiring event for Microsoft Server and Tools</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/great-hiring-event-for-microsoft-server-and-tools</link>
      <pubDate>Mon, 06 Aug 2012 10:08:18 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/08/06/great-hiring-event-for-microsoft-server-and-tools/</guid>
      <description><![CDATA[For more details, please visit https://stbjobs.com/.]]></description>
      <content:encoded><![CDATA[<p>For more details, please visit <a href="https://stbjobs.com/">https://stbjobs.com/</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>How to retrieve all the filters that applies to one particular action in Web API</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-retrieve-all-the-filters-that-applies-to-one-particular-action-in-web-api</link>
      <pubDate>Mon, 09 Jul 2012 23:07:00 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/07/09/how-to-retrieve-all-the-filters-that-applies-to-one-particular-action-in-web-api/</guid>
      <description><![CDATA[Sometimes you want to retrieve all the filters that one action supports, and you can do that via the...]]></description>
      <content:encoded><![CDATA[<p>Sometimes you want to retrieve all the filters that one action supports, and you can do that via the following code snippet. Hope this helps.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="60">
<li><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collection</span>&lt;<span style="color: #2b91af;">FilterInfo</span>&gt; filterInfoCollection = action.GetFilterPipeline(); // here action is action descriptor</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">FilterInfo</span> filterInfo <span style="color: #0000ff;">in</span> filterInfoCollection)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff;">if</span> (filterInfo.Instance <span style="color: #0000ff;">is</span> <span style="color: #2b91af;">MyQueryableAttribute</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;supportQueryable = <span style="color: #0000ff;">true</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">break</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>]]></content:encoded>
    </item>
    <item>
      <title>How to access the Client's IP address in Web API</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-access-the-clients-ip-address-in-web-api</link>
      <pubDate>Mon, 09 Jul 2012 10:07:00 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/07/09/how-to-access-the-clients-ip-address-in-web-api/</guid>
      <description><![CDATA[For&nbsp;web hosted, you can access your client address via the following:

Code...]]></description>
      <content:encoded><![CDATA[<p>For&nbsp;<a></a>web hosted, you can access your client address via the following:</p>
<div style="font-family: 'Courier New', Courier, Monospace; color: #000; font-size: 10pt; border: #000080 1px solid;">
<div style="padding-bottom: 2px; padding-left: 5px; padding-right: 5px; font-family: Verdana, Tahoma, Arial, sans-serif; background: #000080; color: #fff; font-weight: bold; padding-top: 2px;">Code Snippet</div>
<div style="background: #ddd; max-height: 300px; overflow: auto;"><ol style="padding-bottom: 0px; margin: 0px 0px 0px 2.5em; padding-left: 5px; padding-right: 0px; background: #ffffff; padding-top: 0px;" start="36">
<li><span style="color: #0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string</span> clientAddress = <span style="color: #2b91af;">HttpContext</span>.Current.Request.UserHostAddress;</li>
</ol></div>
</div>
<p>For&nbsp;self hosted, you can access your client address via the following:</p>
<div style="font-family: 'Courier New', Courier, Monospace; color: #000; font-size: 10pt; border: #000080 1px solid;">
<div style="padding-bottom: 2px; padding-left: 5px; padding-right: 5px; font-family: Verdana, Tahoma, Arial, sans-serif; background: #000080; color: #fff; font-weight: bold; padding-top: 2px;">Code Snippet</div>
<div style="background: #ddd; max-height: 300px; overflow: auto;"><ol style="padding-bottom: 0px; margin: 0px 0px 0px 2.5em; padding-left: 5px; padding-right: 0px; background: #ffffff; padding-top: 0px;" start="36">
<li><span style="color: #0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object</span> property;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Request.Properties.TryGetValue(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">RemoteEndpointMessageProperty</span>).FullName, <span style="color: #0000ff;">out</span> property);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">RemoteEndpointMessageProperty</span> remoteProperty = property <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">RemoteEndpointMessageProperty</span>;</li>
</ol></div>
</div>
<p>Hope this helps.&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>How to write a custom parameter binding to construct an object either from body or from Uri's query</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-write-a-custom-parameter-binding-to-construct-an-object-either-from-body-or-from-uris-query</link>
      <pubDate>Fri, 11 May 2012 15:16:25 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/05/11/how-to-write-a-custom-parameter-binding-to-construct-an-object-either-from-body-or-from-uris-query/</guid>
      <description><![CDATA[Mike has wrote this really good blog on writing a custom HttpParameterBinding...]]></description>
      <content:encoded><![CDATA[<p>Mike has wrote this really good blog on writing a custom HttpParameterBinding at</p>
<p><a href="https://blogs.msdn.com/b/jmstall/archive/2012/05/11/webapi-parameter-binding-under-the-hood.aspx">https://blogs.msdn.com/b/jmstall/archive/2012/05/11/webapi-parameter-binding-under-the-hood.aspx</a></p>
<p>My blog just want to add a little bit complex example where you might want to have an action which looks like the following:</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="8">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">HomeController</span> : <span style="color: #2b91af;">ApiController</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">HttpGet</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">HttpPost</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> BindCustomComplexTypeFromUriOrBody(<span style="color: #2b91af;">TestItem</span> item)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpResponseMessage</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StringContent</span>(<span style="color: #2b91af;">String</span>.Format(<span style="color: #a31515;">"BindCustomComplexType item.Name = {0}."</span>, item.Name))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">TestItem</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Name { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }</li>
<li style="background: #f3f3f3;">}</li>
</ol></div>
</div>
<p>Then the TestItem object can sometimes come from query like /?Name=Hongmei or coming from body. {Name:Hongmei}. It will be easy to write a custom parameter binding to do that.</p>
<p><strong>Step 1: Write a custom FromUriOrFromBodyParameterBinding</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="16">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">FromUriOrBodyParameterBinding</span> : <span style="color: #2b91af;">HttpParameterBinding</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpParameterBinding</span> _defaultUriBinding;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpParameterBinding</span> _defaultFormatterBinding;</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> FromUriOrBodyParameterBinding(<span style="color: #2b91af;">HttpParameterDescriptor</span> desc)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>(desc)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_defaultUriBinding = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">FromUriAttribute</span>().GetBinding(desc);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_defaultFormatterBinding = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">FromBodyAttribute</span>().GetBinding(desc);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">Task</span> ExecuteBindingAsync(<span style="color: #2b91af;">ModelMetadataProvider</span> metadataProvider, <span style="color: #2b91af;">HttpActionContext</span> actionContext, <span style="color: #2b91af;">CancellationToken</span> cancellationToken)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (actionContext.Request.Content != <span style="color: #0000ff;">null</span> )</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// we have something from the body, try that first</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> _defaultFormatterBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// we need to read things from uri</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> _defaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p>&nbsp;</p>
<p><strong>Step 2:&nbsp;&nbsp;Wire the FromUriOrFromBodyParameterBinding to the configuration</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="52">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #2b91af;">HttpParameterBinding</span> GetCustomParameterBinding(<span style="color: #2b91af;">HttpParameterDescriptor</span> descriptor)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> ( descriptor.ParameterType == <span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">TestItem</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">FromUriOrBodyParameterBinding</span>(descriptor);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// any other types, let the default parameter binding handle</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="26">
<li>config.ParameterBindingRules.Insert(0, GetCustomParameterBinding);</li>
</ol></div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>How to access ClientCertificate in a host agnostic manner</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-access-clientcertificate-in-a-host-agnostic-manner</link>
      <pubDate>Fri, 11 May 2012 14:54:00 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/05/11/how-to-access-clientcertificate-in-a-host-agnostic-manner/</guid>
      <description><![CDATA[&nbsp;When you choose to use Client Certificate as your client credential over SSL, you want to...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;When you choose to use Client Certificate as your client credential over SSL, you want to retrieve the x509 Certificate on the server side to do authorization. It is very simple with the latest Web API bits. If you have access to the request message anywhere in the pipeline, such as message handler, action filter or action. you can simply call the extension method shown below. Please be careful that the GetClientCertificate method might return null if the client does not send an valid certificate.</p>
<p>Now getting the client certificate is easy, but how to set it up correctly on self host and web host is not that easy. The following instructions walk you through the process step by step.</p>
<h3>Web Host</h3>
<p><strong>Step 1: go to the IIS manager, SSL setting, check require client certificate</strong></p>
<p><strong>Step 2: register a custom&nbsp;message handler and verify that the client certificate is something you expect.</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="9">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">CustomCertificateMessageHandler</span> : <span style="color: #2b91af;">DelegatingHandler</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">Task</span>&lt;<span style="color: #2b91af;">HttpResponseMessage</span>&gt; SendAsync(<span style="color: #2b91af;">HttpRequestMessage</span> request, <span style="color: #2b91af;">CancellationToken</span> cancellationToken)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">X509Certificate</span> cert = request.GetClientCertificate();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (cert != <span style="color: #0000ff;">null</span>)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (cert.Subject.Contains(<span style="color: #a31515;">"Some Name you are expecting"</span>))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Thread</span>.CurrentPrincipal = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">GenericPrincipal</span>(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">GenericIdentity</span>(cert.Subject), <span style="color: #0000ff;">new</span>[] { <span style="color: #a31515;">"Administrators"</span> });</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.SendAsync(request, cancellationToken);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p><strong>Step 3: register the custom&nbsp;message handler.</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="30">
<li><span style="color: #008000;">// Register a message to turn the client cert to a admin role</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">GlobalConfiguration</span>.Configuration.MessageHandlers.Add(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">CustomCertificateMessageHandler</span>());</li>
</ol></div>
</div>
<p>&nbsp;</p>
<p><strong>Step 4: Write a RequireAdminAttribute</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="10">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">RequireAdminAttribute</span> : <span style="color: #2b91af;">AuthorizationFilterAttribute</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnAuthorization(<span style="color: #2b91af;">HttpActionContext</span> context)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// do authorization based on the principle.</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">IPrincipal</span> principal = <span style="color: #2b91af;">Thread</span>.CurrentPrincipal;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (principal == <span style="color: #0000ff;">null</span> || !principal.IsInRole(<span style="color: #a31515;">"Administrators"</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.Response = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpResponseMessage</span>(<span style="color: #2b91af;">HttpStatusCode</span>.Unauthorized);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p><strong>Step 5: Add the RequireAdminAttribute to the action.</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="7">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">HomeController</span> : <span style="color: #2b91af;">ApiController</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">RequireAdmin</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> Get()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpResponseMessage</span>(<span style="color: #2b91af;">HttpStatusCode</span>.OK)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StringContent</span>(<span style="color: #a31515;">"Default User"</span>)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">HttpResponseMessage</span> Post()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpResponseMessage</span>(<span style="color: #2b91af;">HttpStatusCode</span>.OK)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StringContent</span>(<span style="color: #a31515;">"User Posted"</span>)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<p>Now your GET&nbsp;action&nbsp;can only be accessed with the administrators.</p>
<h3>Self Host</h3>
<p><strong>Step 1: Tell the WCF transport,&nbsp;I want to client to send certificate over SSL.</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="68">
<li><span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // client cert</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.ClientCredentialType = <span style="color: #2b91af;">HttpClientCredentialType</span>.Certificate;</li>
</ol></div>
</div>
<p><strong>Step 2: Write some client code to pick a certificate to send</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="35">
<li><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebRequestHandler</span> handler = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">WebRequestHandler</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.ClientCertificateOptions = <span style="color: #2b91af;">ClientCertificateOption</span>.Manual; <span style="color: #008000;">// this would pick from the Current user store</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.ClientCertificates.Add(GetClientCertificate());</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.UseDefaultCredentials = <span style="color: #0000ff;">true</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpClient</span> client = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpClient</span>(handler);</li>
</ol></div>
</div>
<p>Implement the GetClientCertificate to retrieve a certificate programmatically.</p>
<p>Then repeat step 2-5 from the web host section to complete the experience.</p>
<p>Hope this helps. &nbsp;</p>
<p>&nbsp;</p>
<p></p>]]></content:encoded>
    </item>
    <item>
      <title>How to support multiple authentication in self host</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-support-multiple-authentication-in-self-host</link>
      <pubDate>Fri, 11 May 2012 14:24:50 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/05/11/how-to-support-multiple-authentication-in-self-host/</guid>
      <description><![CDATA[Since we build web api self host story on top of WCF HttpTransportBindingElement, the multiple...]]></description>
      <content:encoded><![CDATA[<p><span style="font-family: arial,helvetica,sans-serif;">Since we build web api self host story on top of WCF HttpTransportBindingElement, the multiple authentication is not support on WCF for 4.0. Here is the exception you will get if you tried to set the authentication scheme on httptransport binding element. </span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">"System.ArgumentException: The 'Negotiate, Anonymous' authentication scheme has been specified on the HTTP factory. However, the factory only supports specification of exactly one authentication scheme.&nbsp; Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous."</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">The good news is that this has been fixed in .NET framework 4.5 Beta. This is the link <a href="https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx#windows_communication_foundation">https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx#windows_communication_foundation</a>.</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">"Ability to support multiple authentication modes on a single WCF endpoint when using the HTTP transport and transport security.&nbsp;"</span></p>
<p><span style="font-family: arial,helvetica,sans-serif;">&nbsp;So with that fix in mind, web api&nbsp;now provides an extensibility point for people to set the authentication schemes.</span></p>
<p>&nbsp;<strong>Step 1: Derive from HttpSelfHostConfiguration, and register an action to modify TransportBindingElement, and modify the authentication scheme in the action.</strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="77">
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MultipleAuthenticationHttpConfiguration</span> : <span style="color: #2b91af;">HttpSelfHostConfiguration</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> MultipleAuthenticationHttpConfiguration(<span style="color: #0000ff;">string</span> baseAddress)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>(baseAddress)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">BindingParameterCollection</span> OnConfigureBinding(<span style="color: #2b91af;">HttpBinding</span> httpBinding)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httpBinding.ConfigureTransportBindingElement = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Action</span>&lt;<span style="color: #2b91af;">HttpTransportBindingElement</span>&gt;(ConfigureTransportBindingElement);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.OnConfigureBinding(httpBinding);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">void</span> ConfigureTransportBindingElement(<span style="color: #2b91af;">HttpTransportBindingElement</span> bindingElement)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// uncomment the following line if running on 4.5</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// bindingElement.AuthenticationScheme = AuthenticationSchemes.Negotiate | AuthenticationSchemes.Anonymous;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindingElement.AuthenticationScheme = <span style="color: #2b91af;">AuthenticationSchemes</span>.Negotiate;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
</ol></div>
</div>
<pre class="scroll"><strong><code class="csharp"></code></strong>&nbsp;</pre>
<p class="scroll"><strong><code class="csharp">Step 2: use the derived the configuration object instead</code></strong></p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; padding: 2px 5px; color: #ffffff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold;">Code Snippet</div>
<div style="background: #dddddd; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px;" start="24">
<li><span style="color: #0000ff;">var</span> config = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MultipleAuthenticationHttpConfiguration</span>(<span style="color: #2b91af;">Program</span>.BaseAddress);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.Routes.MapHttpRoute(<span style="color: #a31515;">"Default"</span>, <span style="color: #a31515;">"{controller}"</span>, <span style="color: #0000ff;">new</span> { controller = <span style="color: #a31515;">"Sample"</span> });</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetupConfig(config);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpSelfHostServer</span>(config);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server.OpenAsync().Wait();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Service listening at: "</span> + <span style="color: #2b91af;">Program</span>.BaseAddress);</li>
</ol></div>
</div>
<pre class="scroll"><strong></strong></pre>
<pre class="scroll"><strong></strong></pre>]]></content:encoded>
    </item>
    <item>
      <title>How to access HTTPContext from within your Web API action</title>
      <link>https://docs.microsoft.com/archive/blogs/hongmeig1/how-to-access-httpcontext-from-within-your-web-api-action</link>
      <pubDate>Tue, 28 Feb 2012 22:01:20 GMT</pubDate>
      <dc:creator><![CDATA[Hongmei Ge]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/hongmeig1/2012/02/28/how-to-access-httpcontext-from-within-your-web-api-action/</guid>
      <description><![CDATA[Within a Web API action, you can easily access the HttpContext via the HttpContext.Current thread...]]></description>
      <content:encoded><![CDATA[<p>Within a Web API action, you can easily access the HttpContext via the HttpContext.Current thread static. Alternatively,&nbsp;you can also retrieve the context from one of the properties in the Request message.</p>
<div style="font-family: 'Courier New', Courier, Monospace; color: #000; font-size: 10pt; border: #000080 1px solid;">
<div style="padding-bottom: 2px; padding-left: 5px; padding-right: 5px; font-family: Verdana, Tahoma, Arial, sans-serif; background: #000080; color: #fff; font-weight: bold; padding-top: 2px;">Code Snippet</div>
<div style="background: #ddd; max-height: 300px; overflow: auto;"><ol style="padding-bottom: 0px; margin: 0px 0px 0px 2.5em; padding-left: 5px; padding-right: 0px; background: #ffffff; padding-top: 0px;" start="18">
<li><span style="color: #2b91af;">HttpContext</span> context;</li>
<li style="background: #f3f3f3;">Request.Properties.TryGetValue&lt;<span style="color: #2b91af;">HttpContext</span>&gt;(<span style="color: #a31515;">"MS_HttpContext"</span>, <span style="color: #0000ff;">out</span> context);</li>
</ol></div>
</div>
<p>Hope this helps.</p>]]></content:encoded>
    </item>
  </channel>
</rss>