<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Zulfiqar&#039;s weblog</title>
	<atom:link href="http://zamd.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://zamd.net</link>
	<description>Middleware, security &#38; random .Net</description>
	<lastBuildDate>Tue, 21 May 2013 08:23:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='zamd.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Zulfiqar&#039;s weblog</title>
		<link>http://zamd.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://zamd.net/osd.xml" title="Zulfiqar&#039;s weblog" />
	<atom:link rel='hub' href='http://zamd.net/?pushpress=hub'/>
		<item>
		<title>Azure AD OAuth 2.0 Authorization Grant</title>
		<link>http://zamd.net/2013/05/17/azure-ad-oauth-2-0-authorization-grant/</link>
		<comments>http://zamd.net/2013/05/17/azure-ad-oauth-2-0-authorization-grant/#comments</comments>
		<pubDate>Fri, 17 May 2013 23:08:55 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Azure AD]]></category>
		<category><![CDATA[OAuth 2.0]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=487</guid>
		<description><![CDATA[Yesterday I talked about a bug which prevented me to complete the authorization grant flow with Azure AD. It turn out the bug is only exposed when using Azure Management Portal for Relying party registration. In this post, I’ll use Graph Explorer to do the registration which works fine. My scenario is to create a [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=487&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Yesterday I talked about a bug which prevented me to complete the authorization grant flow with Azure AD. It turn out the bug is only exposed when using Azure Management Portal for Relying party registration. In this post, I’ll use <a href="https://graphexplorer.cloudapp.net/">Graph Explorer</a> to do the registration which works fine.</p>
<p>My scenario is to create a simple MVC application which would do the user authentication against the Azure AD.</p>
<p>Once the user is signed in, the web app then acquires an “access” &amp; “refresh token” for the Graph API (I’ll work with other resources in future) using the 3-leg authorization grant flow.</p>
<p>I started by creating an empty MVC 4.0 application and added a home controller with a simple view displaying the identity &amp; claims of the authenticated user.</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/05/image1.png"><img style="display:inline;border:0;" title="image" alt="image" src="http://zuahmed.files.wordpress.com/2013/05/image_thumb1.png?w=244&#038;h=78" width="244" height="78" border="0" /></a></p>
<p>Running the app gave me the url which I would use to register my app with Azure AD using Graph Explorer.  Registration instruction are available in <a href="http://blogs.msdn.com/b/aadgraphteam/archive/2013/05/17/using-oauth-2-0-authorization-code-grant-for-delegated-access-of-directory-via-aad-graph.aspx">this blog post</a> under the ‘Setting up permissions’ section. My registration settings looks like this</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/05/image2.png"><img style="display:inline;border:0;" title="image" alt="image" src="http://zuahmed.files.wordpress.com/2013/05/image_thumb2.png?w=244&#038;h=86" width="244" height="86" border="0" /></a></p>
<p>Now back to VS and using the “Identity &amp; Access”, I have externalized the authentication of my app to windows azure AD.</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/05/image3.png"><img style="display:inline;border:0;" title="image" alt="image" src="http://zuahmed.files.wordpress.com/2013/05/image_thumb3.png?w=244&#038;h=222" width="244" height="222" border="0" /></a></p>
<p>The tooling does all the magic and generates required WIF configuration.</p>
<div class="wlWriterEditableSmartContent" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4a7f640f-54bc-4b9a-81da-42310189d706" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#ffffff;max-height:300px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">system.identityModel</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">identityConfiguration</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">audienceUris</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">add</span><span style="background:#ffffff;color:#ff0000;">value</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://localhost:45906/" rel="nofollow">http://localhost:45906/</a></span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">audienceUris</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">issuerNameRegistry</span><span style="background:#ffffff;color:#ff0000;">type</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">trustedIssuers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">add</span><span style="background:#ffffff;color:#ff0000;">thumbprint</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">3464C5BDD2BE7F2B6112E2F08E9C0024E33D9FE0</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#ff0000;">name</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"><a href="https://sts.windows.net/" rel="nofollow">https://sts.windows.net/</a>{tenantid}/</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">trustedIssuers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">issuerNameRegistry</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">certificateValidation</span><span style="background:#ffffff;color:#ff0000;">certificateValidationMode</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">None</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">/&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">identityConfiguration</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">system.identityModel</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">system.identityModel.services</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">federationConfiguration</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">cookieHandler</span><span style="background:#ffffff;color:#ff0000;">requireSsl</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">false</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">wsFederation</span><span style="background:#ffffff;color:#ff0000;">passiveRedirectEnabled</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">true</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#ff0000;">issuer</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"><a href="https://login.windows.net/common/wsfed" rel="nofollow">https://login.windows.net/common/wsfed</a></span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#ff0000;">realm</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://localhost:45906/" rel="nofollow">http://localhost:45906/</a></span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#ff0000;">reply</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://localhost:45906/" rel="nofollow">http://localhost:45906/</a></span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#ff0000;">requireHttps</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;">false</span><span style="background:#ffffff;color:#000000;">&#8220;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">federationConfiguration</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />
<span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">system.identityModel.services</span><span style="background:#ffffff;color:#0000ff;">&gt;</span></div>
</div>
</div>
<p>Now when I run my application, I’m redirected to azure AD for authentication and after signing in, I’m back in my app with following claims.</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/05/image4.png"><img style="display:inline;border:0;" title="image" alt="image" src="http://zuahmed.files.wordpress.com/2013/05/image_thumb4.png?w=244&#038;h=175" width="244" height="175" border="0" /></a></p>
<p>The Authorize action simply redirects to the authorization endpoint requesting an ‘authorization code’.</p>
<div class="wlWriterEditableSmartContent" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:96d9bbfc-986e-4f10-b1fb-70454fab92d7" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Acquire authorization code</div>
<div style="background-color:#ffffff;max-height:300px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#ffffff;color:#0000ff;">public</span><span style="background:#ffffff;color:#2b91af;">ActionResult</span><span style="background:#ffffff;color:#000000;"> Authorize()</span><br />
<span style="background:#ffffff;color:#000000;">{</span><br />
<span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> @params = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#2b91af;">NameValueCollection</span><br />
<span style="background:#ffffff;color:#000000;">{</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;response_type&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;code&#8221;}</span><span style="background:#ffffff;color:#000000;">,</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;client_id&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;87638e3d-6b56-46b6-946f-8b3b9fa6f04e&#8221;}</span><span style="background:#ffffff;color:#000000;">,</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;resource&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;<a href="https://graph.windows.net&#038;#8221" rel="nofollow">https://graph.windows.net&#038;#8221</a>;}</span><span style="background:#ffffff;color:#000000;">,</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;redirect_uri&#8221;</span><span style="background:#ffffff;color:#000000;">, Url.Action(</span><span style="background:#ffffff;color:#a31515;">&#8220;Authorized&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#0000ff;">null</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#0000ff;">null</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;http&#8221;</span><span style="background:#ffffff;color:#000000;">)}</span><br />
<span style="background:#ffffff;color:#000000;">};</span><br />
<span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> query = </span><span style="background:#ffffff;color:#2b91af;">HttpUtility</span><span style="background:#ffffff;color:#000000;">.ParseQueryString(</span><span style="background:#ffffff;color:#a31515;">&#8220;&#8221;</span><span style="background:#ffffff;color:#000000;">);</span><br />
<span style="background:#ffffff;color:#000000;">query.Add(@params);</span>    <span style="background:#ffffff;color:#0000ff;">return</span><span style="background:#ffffff;color:#000000;"> Redirect(</span><span style="background:#ffffff;color:#2b91af;">Constants</span><span style="background:#ffffff;color:#000000;">.AzureADAuthorizationEndpoint + </span><span style="background:#ffffff;color:#a31515;">&#8220;?&#8221;</span><span style="background:#ffffff;color:#000000;"> + query);</span><br />
<span style="background:#ffffff;color:#000000;">}</span></p>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>I’m passing <span style="text-decoration:underline;">Authorize<strong>d</strong></span> action as the redirect_uri to Azure AD. That’s where AD would send me the ‘authorization code&#8217;.</p>
<p>Clicking the ‘Authorize’ link takes me to Azure Ad &amp; after authentication AD redirects my browser back to Authorize<strong>d</strong> action with an authorization code.</p>
<p><a title="http://localhost:45906/Home/Authorized?code=AAAAAAAAcJ3MFRmxlJAB5CwTBrXmI7t8zdfTrUmfPCbGHQCxbWXOxLieUD4r_fah-rnOwQkxS6B_KOSdLhjsf6n5sQghMwu9ynqFD2-qHBfRkxE3DnS8htlHaDqCdG-Wa5MvbRXZeCKUFr-k37n5Mn4T2KCdKx-nkyNheqZJmwmDFWwix_Gi_QJohhUMk-SDyrnV4Jy-tT_gfiKwKRzVi31JmsJV_b8u-5p398GsGfxBlfxuWuNYuUiUOUDdfLQCYbK0urn0HChsndnPuTZJxtJtNH7WdWnUdha108kVctvHW4u8IihV9P10OsM1gT_D67f00SjVIAA" href="http://localhost:45906/Home/Authorized?code=AAAAAAAAcJ3MFRmxlJAB5CwTBrXmI7t8zdfTrUmfPCbGHQCxbWXOxLieUD4r_fah-rnOwQkxS6B_KOSdLhjsf6n5sQghMwu9ynqFD2-qHBfRkxE3DnS8htlHaDqCdG-Wa5MvbRXZeCKUFr-k37n5Mn4T2KCdKx-nkyNheqZJmwmDFWwix_Gi_QJohhUMk-SDyrnV4Jy-tT_gfiKwKRzVi31JmsJV_b8u-5p398GsGfxBlfxuWuNYuUiUOUDdfLQCYbK0urn0HChsndnPuTZJxtJtNH7WdWnUdha108kVctvHW4u8IihV9P10OsM1gT_D67f00SjVIAA">http://localhost:45906/Home/Authorized?code=AAAAAAAAcJ3MFRmxlJAB5CwTBrXmI7t8zdfTrUmfPCbGHQCxbWXOxLieUD4r_fah-rnOwQkxS6B_KOSdLhjsf6n5sQghMwu9ynqFD2-qHBfRkxE3DnS8htlHaDqCdG-Wa5MvbRXZeCKUFr-k37n5Mn4T2KCdKx-nkyNheqZJmwmDFWwix_Gi_QJohhUMk-SDyrnV4Jy-tT_gfiKwKRzVi31JmsJV_b8u-5p398GsGfxBlfxuWuNYuUiUOUDdfLQCYbK0urn0HChsndnPuTZJxtJtNH7WdWnUdha108kVctvHW4u8IihV9P10OsM1gT_D67f00SjVIAA</a></p>
<p>From the Authorize<strong>d</strong> controller action I fire a post request which gives me access and refresh tokens back. Please note, the redirect_uri MUST be same for both authorization code and token request.</p>
<div class="wlWriterEditableSmartContent" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5efb38a8-e102-4eb5-a5b6-9bfdfb2ff8e0" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Exchange code with tokens</div>
<div style="background-color:#ffffff;max-height:300px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#ffffff;color:#0000ff;">public</span><span style="background:#ffffff;color:#0000ff;">async</span><span style="background:#ffffff;color:#2b91af;">Task</span><span style="background:#ffffff;color:#000000;">&lt;</span><span style="background:#ffffff;color:#2b91af;">ActionResult</span><span style="background:#ffffff;color:#000000;">&gt; Authorized(</span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;"> code)</span><br />
<span style="background:#ffffff;color:#000000;">{</span><br />
<span style="background:#ffffff;color:#0000ff;">if</span><span style="background:#ffffff;color:#000000;"> (</span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;">.IsNullOrEmpty(code))</span><br />
<span style="background:#ffffff;color:#000000;">RedirectToAction(</span><span style="background:#ffffff;color:#a31515;">&#8220;Index&#8221;</span><span style="background:#ffffff;color:#000000;">);</span>    <span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> data = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#2b91af;">Dictionary</span><span style="background:#ffffff;color:#000000;">&lt;</span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;">&gt;</span><br />
<span style="background:#ffffff;color:#000000;">{</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;grant_type&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;authorization_code&#8221;}</span><span style="background:#ffffff;color:#000000;">,</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;client_id&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;87638e3d-6b56-46b6-946f-8b3b9fa6f04e&#8221;}</span><span style="background:#ffffff;color:#000000;">,</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;redirect_uri&#8221;</span><span style="background:#ffffff;color:#000000;">, Url.Action(</span><span style="background:#ffffff;color:#a31515;">&#8220;Authorized&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#0000ff;">null</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#0000ff;">null</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;http&#8221;</span><span style="background:#ffffff;color:#000000;">)},</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;client_secret&#8221;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&#8220;V/7FTVm****************UQi6MkbwhmBqKxz0=&#8221;}</span><span style="background:#ffffff;color:#000000;">,</span><br />
<span style="background:#ffffff;color:#000000;">{</span><span style="background:#ffffff;color:#a31515;">&#8220;code&#8221;</span><span style="background:#ffffff;color:#000000;">, code}</span><br />
<span style="background:#ffffff;color:#000000;">};</span></p>
<p><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> client = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#2b91af;">HttpClient</span><span style="background:#ffffff;color:#000000;">(</span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#2b91af;">WebRequestHandler</span><span style="background:#ffffff;color:#000000;">());</span><br />
<span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> response = </span><span style="background:#ffffff;color:#0000ff;">await</span><span style="background:#ffffff;color:#000000;"> client.PostAsync(</span><span style="background:#ffffff;color:#2b91af;">Constants</span><span style="background:#ffffff;color:#000000;">.AzureADTokenEndpoint, </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#2b91af;">FormUrlEncodedContent</span><span style="background:#ffffff;color:#000000;">(data));</span><br />
<span style="background:#ffffff;color:#000000;">response.EnsureSuccessStatusCode();</span></p>
<p><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> msg = </span><span style="background:#ffffff;color:#0000ff;">await</span><span style="background:#ffffff;color:#000000;"> response.Content.ReadAsStringAsync();</span><br />
<span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> tokenResponse = </span><span style="background:#ffffff;color:#2b91af;">JsonConvert</span><span style="background:#ffffff;color:#000000;">.DeserializeObject&lt;</span><span style="background:#ffffff;color:#2b91af;">TokenResponse</span><span style="background:#ffffff;color:#000000;">&gt;(msg);</span><br />
<span style="background:#ffffff;color:#0000ff;">return</span><span style="background:#ffffff;color:#000000;"> View(tokenResponse);</span><br />
<span style="background:#ffffff;color:#000000;">}</span></p>
</div>
</div>
</div>
<p><a href="http://zuahmed.files.wordpress.com/2013/05/image5.png"><img style="display:inline;border:0;" title="image" alt="image" src="http://zuahmed.files.wordpress.com/2013/05/image_thumb5.png?w=244&#038;h=95" width="244" height="95" border="0" /></a></p>
<p>The access &amp; refresh tokens are scoped to Graph API in this case. I can now attach this “access token” to my requests to graph API to read/write the directory data. There are few samples on this topic already so I’m not going to cover that in this post.</p>
<p>Source: <a href="http://sdrv.ms/13u8iid">HomeController.cs</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/487/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=487&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/05/17/azure-ad-oauth-2-0-authorization-grant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/05/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/05/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Azure AD OAuth 2.0 Client_Credentials Flow</title>
		<link>http://zamd.net/2013/05/16/azure-ad-oauth-2-0-client_credentials-flow/</link>
		<comments>http://zamd.net/2013/05/16/azure-ad-oauth-2-0-client_credentials-flow/#comments</comments>
		<pubDate>Thu, 16 May 2013 22:24:18 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Azure AD]]></category>
		<category><![CDATA[OAuth 2.0]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=475</guid>
		<description><![CDATA[I was playing with the Authorization code grant type recently added to Azure Active directory however there is bug in the preview implementation which prevents exchange an ‘authorization code’ with an access token. I can get the authorization code for graph api by using following url in the browser. https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/authorize?response_type=code&#38;client_id=9f030b74-1ec1-4b6b-8911-f4e6e465ff9d&#38;resource=https://graph.windows.net&#38;api-version=1.0 AAD authenticates me and redirects [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=475&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was playing with the Authorization code grant type recently added to Azure Active directory however there is bug in the preview implementation which prevents exchange an ‘authorization code’ with an access token.</p>
<p>I can get the authorization code for graph api by using following url in the browser. </p>
<p><a href="https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/authorize?response_type=code&amp;client_id=9f030b74-1ec1-4b6b-8911-f4e6e465ff9d&amp;resource=https://graph.windows.net&amp;api-version=1.0">https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/authorize?response_type=code&amp;client_id=9f030b74-1ec1-4b6b-8911-f4e6e465ff9d&amp;resource=https://graph.windows.net&amp;api-version=1.0</a></p>
<p>AAD authenticates me and redirects with an authorization code below.</p>
<p><a href="http://localhost/?code=AAAAAAAAfgMZDe1KgG-yqkz7_upts_gtuQ5RgzoM71bkz3PKJo312flYTAp8PrpaUnl3nMaRF_asmQnEZpMjQSflFRJoFl0nVO5HyGnyRG2haLQAM7kz0wrW3I_D2s5FResTcc2g3hnStrVLWFoAovnw6gjh7Imir3Zistm7ZuJgFkbzMJp8Q1Nm1ZCoqW8W9lDDZtdizd9uI6ALo04OW9jp4bjW9KXshy2x6Nz41N35RzXD2DGiAJP5PqtCB1q54wmRsMauPtKrBxU7Nd3T3cbUV5XJxdNojZWaz-ER-tWYySVNMwLZ0gcRzIp-FMWy3Y7oGsHEpAWrrpmnV3Nx6K2Wb_BIXW-EI6HAQX57HDCFVmhBR8cgAA">http://localhost/?code=AAAAAAAAfgMZDe1KgG-yqkz7_upts_gtuQ5RgzoM71bkz3PKJo312flYTAp8PrpaUnl3nMaRF_asmQnEZpMjQSflFRJoFl0nVO5HyGnyRG2haLQAM7kz0wrW3I_D2s5FResTcc2g3hnStrVLWFoAovnw6gjh7Imir3Zistm7ZuJgFkbzMJp8Q1Nm1ZCoqW8W9lDDZtdizd9uI6ALo04OW9jp4bjW9KXshy2x6Nz41N35RzXD2DGiAJP5PqtCB1q54wmRsMauPtKrBxU7Nd3T3cbUV5XJxdNojZWaz-ER-tWYySVNMwLZ0gcRzIp-FMWy3Y7oGsHEpAWrrpmnV3Nx6K2Wb_BIXW-EI6HAQX57HDCFVmhBR8cgAA</a></p>
<p>At this stage, I should be able to exchange this ‘code’ with an ‘access token’ &amp; a refresh token by issuing following POST request via fiddler. </p>
<p><a href="https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/token">&#8216;https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/token</a></p>
<p>User-Agent: Fiddler</p>
<p>Content-Type: application/x-www-form-urlencoded</p>
<p>Host: login.windows.net</p>
<p>Content-Length: 546</p>
<p>grant_type=authorization_code&amp;client_id=9f030b74-1ec1-4b6b-8911-f4e6e465ff9d&amp;redirect_uri=http://localhost/&amp;code=AAAAAAAAU1SjeRieJ2c2QVOayghJbBw-kihXTp7BrG10v73WsqEzFVgsgm3SfYv64_mT9WXFU02vvoVY8J7AxuAfcbJ9fysCW3TI3zixbrwPoz2rZruWJVMT5J7XSI34nxRA13kXIvwf8Sbv7EALsJw-2V56jCFz1sXNwQkkw7UBWX72qvlCP6CuhX0Rqw6LI_4Uqh5dk9D6MLNk02ttTyKnrMkFqV0eN96DhuYZXYD_dF331of8_AULAvkGNGwQzCcqCpws57X8l2rmczkDjvXTAQD2yC6Pb_Hq7eBNNxvFHKTR0D9gquwmPsXVY_jDae2nOa7fI4JdNM92Nto0UFLVoVnIuhxGhhIQzxLz9qb_b-I6fnUgAA&amp;client_secret=V62j8vqo4pRHpCSOK/+jSa1ww=+V62j8vqo4pRHpCSOK/+jSa1ww=</p>
<p>However doing this results in a ‘ACS50000: There was an error issuing a token. ACS70001: Error validating credentials. ACS50012: Invalid client secret is provided’ error. I’ll do a follow up post when this bug is fixed. </p>
<p>My second choice was to use the simple client_credentials (also known as two-leg) flow. </p>
<p>This time I used fiddler to craft a POST request to directly acquire a token from AAD OAuth 2.0 endpoint. </p>
<p><a title="https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/token?api-version=1.0" href="https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/token?api-version=1.0">https://login.windows.net/69383356-56dd-4e78-a18e-a4ff5450c995/oauth2/token?api-version=1.0</a></p>
<p>User-Agent: Fiddler   <br />Content-Type: application/x-www-form-urlencoded    <br />Host: login.windows.net    <br />Content-Length: 178</p>
<p>grant_type=client_credentials&amp;client_id=9f030b74-1ec1-33s3-8911-f4e6e465ff9d&amp;client_secret=V62j8vqo4pRHpCSOK/+jSa1ww=+V62j8vqo4pRHpCSOK/+jSa1ww=&amp;resource=http%3A%2F%2Flocalhost%2F</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/05/image.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/05/image_thumb.png?w=1326&#038;h=200" width="1326" height="200" /></a> </p>
<p>I got 200 OK with JWT token as the payload. I can now attach this token to my REST services where I can process it using the WIF JWTTokenHandler extension as shown below:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b0cf444a-d8d4-4cd5-87b1-a69a4151fb39" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">static</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#0000ff;">void</span><span style="background:#ffffff;color:#000000;"> Main(</span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;">[] args)</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">{</span></li>
<li>    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> handler = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">JWTSecurityTokenHandler</span><span style="background:#ffffff;color:#000000;">();</span></li>
<li style="background:#f3f3f3;">    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> token = (</span><span style="background:#ffffff;color:#2b91af;">JWTSecurityToken</span><span style="background:#ffffff;color:#000000;">)</span></li>
<li>    <span style="background:#ffffff;color:#000000;">handler.ReadToken(</span></li>
<li style="background:#f3f3f3;">        <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#a31515;">&quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1THdqcHdBSk9NOW4tQSJ9.eyJhdWQiOiJodHRwOi8vbG9jYWxob3N0LyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzY5MzgzMzU2LTU2ZGQtNGU3OC1hMThlLWE0ZmY1NDUwYzk5NS8iLCJuYmYiOjEzNjg3Mzk2NTYsImV4cCI6MTM2ODc4Mjg1Niwic3ViIjoiY2MyMjQ1NTQtZDhlZC00YTY0LThmZjUtOTJiZjNiYjEzZDkyIiwiYXBwaWQiOiI5ZjAzMGI3NC0xZWMxLTRiNmItODkxMS1mNGU2ZTQ2NWZmOWQiLCJvaWQiOiJjYzIyNDU1NC1kOGVkLTRhNjQtOGZmNS05MmJmM2JiMTNkOTIiLCJ0aWQiOiI2OTM4MzM1Ni01NmRkLTRlNzgtYTE4ZS1hNGZmNTQ1MGM5OTUiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC82OTM4MzM1Ni01NmRkLTRlNzgtYTE4ZS1hNGZmNTQ1MGM5OTUvIn0.BLtqbzU5pEyn5c6ubQxu2UPzoCd_I9Rokycq4LqThWGkdAy9vL3vqptAHXlKOTK-VFPkarfJ1Jui-GaiGZE_BKLFW0x_cxv4bTx_fAktTsDK51iv9wD8jYuftrUWaaqoonD29SQxRmic_r38LBqJwQIJRO4IfMUeMLmgYQ7B1DQs24D9oSx36pyc7CzX3sZH-nfbNPF4z8wUHrX0zzf7KwWCu5RhK6wmXKbiNKaMIw3VzTq6KsEbqFBV-3IuGFSGadrUfpJG0KZrEc3ZhNJ_gEWuBwhwTKtaVrWQ3_1wyxTtdKG1dPuVZmFxKCIfOJkqsvTFZKD4bECv5DJfvyhzlQ&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li>    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> validationParams = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">TokenValidationParameters</span></li>
<li style="background:#f3f3f3;">                               <span style="background:#ffffff;color:#000000;">{</span></li>
<li>                                   <span style="background:#ffffff;color:#000000;">AudienceUriMode = </span><span style="background:#ffffff;color:#2b91af;">AudienceUriMode</span><span style="background:#ffffff;color:#000000;">.Never,</span></li>
<li style="background:#f3f3f3;">                                   <span style="background:#ffffff;color:#000000;">SigningTokenResolver = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">HardcodedCertResolver</span><span style="background:#ffffff;color:#000000;">(),</span></li>
<li>                                   <span style="background:#ffffff;color:#000000;">ValidateIssuer = </span><span style="background:#ffffff;color:#0000ff;">false</span></li>
<li style="background:#f3f3f3;">                               <span style="background:#ffffff;color:#000000;">};</span></li>
<li>    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> ci = handler.ValidateToken(token,validationParams);</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="background:#ffffff;color:#000000;">ci.Claims.ToList().ForEach(c =&gt;</span></li>
<li style="background:#f3f3f3;">                               <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">Console</span><span style="background:#ffffff;color:#000000;">.WriteLine(</span><span style="background:#ffffff;color:#a31515;">&quot;{</span><span style="background:#ffffff;color:#3cb371;">0}</span><span style="background:#ffffff;color:#a31515;"> = {</span><span style="background:#ffffff;color:#3cb371;">1}</span><span style="background:#ffffff;color:#a31515;">&quot;</span><span style="background:#ffffff;color:#000000;">, c.Type, c.Value));</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">Console</span><span style="background:#ffffff;color:#000000;">.WriteLine(token.ValidTo);</span></li>
<li>    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">Console</span><span style="background:#ffffff;color:#000000;">.ReadLine();</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">}</span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>Hope that helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/475/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=475&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/05/16/azure-ad-oauth-2-0-client_credentials-flow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>CRM Online Entity Creation</title>
		<link>http://zamd.net/2013/04/30/crm-online-entity-creation/</link>
		<comments>http://zamd.net/2013/04/30/crm-online-entity-creation/#comments</comments>
		<pubDate>Tue, 30 Apr 2013 14:42:11 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Dynamics CRM]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=471</guid>
		<description><![CDATA[Recently I started exploring the world of Dynamics and specifically Dynamics CRM. Technically the platform looks fairly simple with a reasonably clean web services API (mostly SOAP) &#38; SAML based message security (remember legacy WS-Trust ) using Live ID as the identity provider. The helper code from the sdk (\sdk\samplecode\cs\helpercode) hides all the complexity but [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=471&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Recently I started exploring the world of Dynamics and specifically Dynamics CRM. Technically the platform looks fairly simple with a reasonably clean web services API (mostly SOAP) &amp; SAML based message security (remember <em>legacy</em> WS-Trust <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) using Live ID as the identity provider.</p>
<p>The helper code from the sdk (\sdk\samplecode\cs\helpercode) hides all the complexity but the under the hood following flow happens to interact with Dynamics Online Web Services. </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/04/image.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/04/image_thumb.png?w=244&#038;h=123" width="244" height="123" /></a> </p>
<p>Following is small console application I used to create a new lead into CRM Online.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1a3a86f1-f07a-4f89-a3bc-b275b18b4a99" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Code Snippet</div>
<div style="background-color:#ffffff;max-height:300px;overflow:auto;padding:2px 5px;">    <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> connection =</span><br />         <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">CrmConnection</span><span style="background:#ffffff;color:#000000;">.Parse(</span><span style="background:#ffffff;color:#a31515;">&quot;Url=https://psfd365.crm4.dynamics.com; User ID=zamd@psfd365.onmicrosoft.com; Password=*password*;&quot;</span><span style="background:#ffffff;color:#000000;">);</span><br />     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> organization = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">OrganizationService</span><span style="background:#ffffff;color:#000000;">(connection);</span></p>
<p>     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> who = organization.Execute(</span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">WhoAmIRequest</span><span style="background:#ffffff;color:#000000;">()) </span><span style="background:#ffffff;color:#0000ff;">as</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">WhoAmIResponse</span><span style="background:#ffffff;color:#000000;">;</span><br />     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">Console</span><span style="background:#ffffff;color:#000000;">.WriteLine(</span><span style="background:#ffffff;color:#a31515;">&quot;{</span><span style="background:#ffffff;color:#3cb371;">0}</span><span style="background:#ffffff;color:#a31515;">@{</span><span style="background:#ffffff;color:#3cb371;">1}</span><span style="background:#ffffff;color:#a31515;">&quot;</span><span style="background:#ffffff;color:#000000;">, who.UserId, who.OrganizationId);</span><br />     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> user = </span><br />     <span style="background:#ffffff;color:#000000;">organization.Retrieve(</span><span style="background:#ffffff;color:#a31515;">&quot;systemuser&quot;</span><span style="background:#ffffff;color:#000000;">, who.UserId,</span><br />                           <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">ColumnSet</span><span style="background:#ffffff;color:#000000;">(</span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;">[] {</span><span style="background:#ffffff;color:#a31515;">&quot;firstname&quot;</span><span style="background:#ffffff;color:#000000;">, </span><span style="background:#ffffff;color:#a31515;">&quot;lastname&quot;}</span><span style="background:#ffffff;color:#000000;">)) </span><span style="background:#ffffff;color:#0000ff;">as</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">SystemUser</span><span style="background:#ffffff;color:#000000;">;</span></p>
<p>     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">Console</span><span style="background:#ffffff;color:#000000;">.WriteLine(</span><span style="background:#ffffff;color:#a31515;">&quot;creating lead&#8230;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></p>
<p>     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> newLead = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#2b91af;">Lead</span><br />                        <span style="background:#ffffff;color:#000000;">{</span><br />                            <span style="background:#ffffff;color:#000000;">Subject = </span><span style="background:#ffffff;color:#a31515;">&quot;Interested in dyanamics crm&#8230;&quot;</span><span style="background:#ffffff;color:#000000;">,</span><br />                            <span style="background:#ffffff;color:#000000;">FirstName = user.FirstName,</span><br />                            <span style="background:#ffffff;color:#000000;">LastName = user.LastName,</span><br />                            <span style="background:#ffffff;color:#000000;">MobilePhone = </span><span style="background:#ffffff;color:#a31515;">&quot;004412123231212&quot;</span><br />                        <span style="background:#ffffff;color:#000000;">};</span></p>
<p>     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> leadId = organization.Create(newLead);</span><br />     <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#2b91af;">Console</span><span style="background:#ffffff;color:#000000;">.WriteLine(</span><span style="background:#ffffff;color:#a31515;">&quot;New lead {</span><span style="background:#ffffff;color:#3cb371;">0}</span><span style="background:#ffffff;color:#a31515;"> created.&quot;</span><span style="background:#ffffff;color:#000000;">,leadId);</span><br /> <span style="background:#ffffff;color:#000000;">}</span></div>
</p></div>
</p></div>
</p>
<p>that’s it – you can see the new lead created below. </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/04/image1.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/04/image_thumb1.png?w=244&#038;h=132" width="244" height="132" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/471/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=471&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/04/30/crm-online-entity-creation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/04/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/04/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>WF Security Pack Update</title>
		<link>http://zamd.net/2013/03/13/wf-security-pack-update/</link>
		<comments>http://zamd.net/2013/03/13/wf-security-pack-update/#comments</comments>
		<pubDate>Wed, 13 Mar 2013 01:01:19 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[WF4.5]]></category>
		<category><![CDATA[WFSP]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=461</guid>
		<description><![CDATA[Quite a few folks have asked me about updating WF Security Pack to .NET 4.5 as WIF is now integrated into .NET 4.5. Today I manage to spare sometime to upgrade the WFSP to .NET 4.5/WIF 4.5.&#160; I have also pushed the updated source code to github which you can pull down from https://github.com/zamd/wfsp/ Please [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=461&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Quite a few folks have asked me about updating WF Security Pack to .NET 4.5 as WIF is now integrated into .NET 4.5. </p>
<p>Today I manage to spare sometime to upgrade the WFSP to .NET 4.5/WIF 4.5.&#160; I have also pushed the updated source code to github which you can pull down from <a title="https://github.com/zamd/wfsp/" href="https://github.com/zamd/wfsp/">https://github.com/zamd/wfsp/</a></p>
<p>Please note <a href="https://github.com/zamd/wfsp/">github version</a> of the codebase is different from <a href="http://wf.codeplex.com/releases/view/48114">codeplex</a>, which was refactored by a WF team member. The github version of the source code came straight from laptop. I intend to create a Nuget package and potentially a Visual Studio Extension as well. Stay tuned… </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/461/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=461&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/03/13/wf-security-pack-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>
	</item>
		<item>
		<title>Federating Office 365 (Azure Active Directory) with a Custom STS</title>
		<link>http://zamd.net/2013/02/08/federating-a-custom-sts-with-office-365-azure-active-directory/</link>
		<comments>http://zamd.net/2013/02/08/federating-a-custom-sts-with-office-365-azure-active-directory/#comments</comments>
		<pubDate>Fri, 08 Feb 2013 16:07:32 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Azure AD]]></category>
		<category><![CDATA[Office 365]]></category>
		<category><![CDATA[SSO]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=453</guid>
		<description><![CDATA[Let’s start of with a clarification: As of today, federating Office 365 (Azure AD) with a Custom STS is NOT supported by Microsoft.&#160; Today the only supported STSs are AD FS 2.0, Shibboleth 2, Optimal IDM Federation Services and PingFederate 6.10. With that cleared, Office 365 STS supports both WS-Federation &#38; SAML protocols for user [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=453&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Let’s start of with a clarification: <strong>As of today, federating Office 365 (Azure AD) with a Custom STS is NOT supported by Microsoft.&#160; </strong>Today the only supported STSs are AD FS 2.0, Shibboleth 2, Optimal IDM Federation Services and PingFederate 6.10. </p>
<p>With that cleared, Office 365 STS supports both WS-Federation &amp; SAML protocols for user authentication which means technically any compatible STS can be used as the Identity Provider STS for Office 365 services or other Relying Parties with a trust relationship with Azure Active Directory. </p>
<p>Azure AD supports In-cloud &amp; Federated Identities.</p>
<p>With In-Cloud identities all user information, including the passwords, are stored in the online directory.</p>
<p>With Federating identities, only basic information is stored in online directory (as shadow accounts) and user identities are mastered in on-premise directories. Passwords are never copied to online directory and Azure AD relies on federation for user sign in. </p>
<p>A key prerequisite for Office 365 SSO is to create federated identities (shadow accounts) in Azure AD and there are different options/tools to do this. </p>
<ol>
<li>DirSync is the recommended tool but it only supports Active Directory as the identity source. DirSync &amp; AD FS 2.0 are the primary tools to enable federation between an on-premises AD and Azure AD.</li>
<li>Graph API is a new RESTful API to manage online directory and looks very promising for creating cloud-only identities. Graph API today doesn’t support creating Federated identities. </li>
<li>MSOL PowerShell cmdlets: These cmdlets use the SOAP based Provisioning Service and are functionally quite rich. They support most of the operations including the creation of federated identities. I have used these cmdlet for my scenairo. Few commerical tools also wrap these cmdlets to perform various Office 365 provisioning operations. </li>
<li>Forfront Identity Manager (FIM) is another potential option which can create Federated accounts from source directories other than AD but I haven’t explored that in detail. </li>
</ol>
<p>Now once you have the federated identities provisioned (or synced from your on-premises user identity store) in Azure AD, the next step is to establish a trust relationship between Azure AD and your custom STS. This is assuming you have already done the domain verification etc.</p>
<p>I have used Set-MsolDomainAuthentication cmdlet for this. </p>
<p>Set-MsolDomainAuthentication –DomainName bccoss.com –federationBrandName bccoss.com -Authentication Federated&#160; -PassiveLogOnUri $url -SigningCertificate $certData -IssuerUri $uri -ActiveLogOnUri $ecpUrl -LogOffUri $logoutUrl -PreferredAuthenticationProtocol WSFed</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image3.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb3.png?w=210&#038;h=244" width="210" height="244" /></a>&#160;</p>
<p>At this stage, if I browse to the Microsoft Online Services portal (<a href="http://portal.microsoftonline.com/">http://portal.microsoftonline.com/</a>) and choose to login using my federated domain (@bccoss.com) – I got redirected to my custom STS. </p>
<p>In this case, I’m using Thinktecture STS but that doesn’t work out of box with Office 365 / Azure AD so I have to modify the STS to make it compatible with Azure AD. I’ll explain the Office 365 compatibility requirements of an STS in a future post. </p>
<p>I’ll also try to contribute my Thinktecture modification to code back to git at some point. </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image4.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb4.png?w=244&#038;h=188" width="244" height="188" /></a> <a href="http://zuahmed.files.wordpress.com/2013/02/image5.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb5.png?w=244&#038;h=188" width="244" height="188" /></a> </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image6.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb6.png?w=244&#038;h=188" width="244" height="188" /></a> <a href="http://zuahmed.files.wordpress.com/2013/02/image7.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb7.png?w=244&#038;h=188" width="244" height="188" /></a> </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image8.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb8.png?w=244&#038;h=188" width="244" height="188" /></a> <a href="http://zuahmed.files.wordpress.com/2013/02/image9.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb9.png?w=244&#038;h=188" width="244" height="188" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/453/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=453&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/02/08/federating-a-custom-sts-with-office-365-azure-active-directory/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Custom STS for Sitefinity 5.x</title>
		<link>http://zamd.net/2013/02/06/custom-sts-for-sitefinity-5-x/</link>
		<comments>http://zamd.net/2013/02/06/custom-sts-for-sitefinity-5-x/#comments</comments>
		<pubDate>Wed, 06 Feb 2013 21:38:13 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Federation/STS]]></category>
		<category><![CDATA[Sitefinity]]></category>
		<category><![CDATA[SSO]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=438</guid>
		<description><![CDATA[Sitefinity 5.x introduced claims based security &#38; Single-Sign-On features based on a simple HTTP redirect based token issuance protocol which I’m going to call ‘Sitefinity sign-in protocol’ in my posts. Version 5.x has also standardized on using Simple Web Token (SWT) as the default token format for user authentication and SSO needs. Sitefinity 5.x comes [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=438&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Sitefinity 5.x introduced claims based security &amp; Single-Sign-On features based on a simple HTTP redirect based token issuance protocol which I’m going to call ‘<strong>Sitefinity sign-in protocol’ </strong>in my posts. Version 5.x has also standardized on using Simple Web Token (SWT) as the default token format for user authentication and SSO needs. </p>
<p>Sitefinity 5.x comes with a built-in local STS which authenticates users using the standard membership authentication and issue SWT tokens in accordance with Sitefinity sign-in protocol. Sitefinity doesn’t have a hard dependency on this built-in STS rather it relies on it’s sign-in protocol and SWT token format which means we can introduce a custom STS in the mix and Sitefinity would happily work with our Custom STS which obviously has to adhere to Sitfinity sign-in protocol and token format. </p>
<p>This STS based design in Sitefinity 5.x could enable many SSO scenarios, some of which I’m going to explore in future posts. Following are examples of few possibilities: </p>
<ul>
<li>I can create a Custom STS and then have multiple applications (RPs <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) including Sitefinity 5.x trust this single STS, which would enable the users to single sign-on across all those applications. </li>
<li>I can create a multi-protocol STS which can enable user SSO across workloads/products. For example, SSO between Sitefinity &amp; Office 365 or another portals, speaking the SAML protocol. </li>
</ul>
<p>For now, I’ll show you how to use a custom STS with Sitefinity for user authentication. I have already developed and deployed a Sitefinity compatible STS @ <a href="http://sts.pilesoft.com">http://sts.pilesoft.com</a> while Sitefinity is running @ <a href="http://pilesoft.com">http://pilesoft.com</a>.</p>
<p><strong>Step 1:</strong> Register custom STS with Sitefinity so that it can trust the token issued by custom STS.</p>
<p>Open the .\App_Data\Sitefinity\Configuration\SecurityConfig.config file and locate the &lt;securityTokenIssuers&gt; element and following line to &lt;securityTokenIssuers&gt; element.</p>
<p>&lt;add key=&quot;CD29559E6EDC312272976AC43F7E921C5766D7063DAF6D177F3EEDEB1802FABE&quot; encoding=&quot;Hexadecimal&quot; membershipProvider=&quot;Default&quot; realm=&quot;<a href="http://sts.pilesoft.com&quot;/">http://sts.pilesoft.com&quot;/</a>&gt;</p>
<p>Your config should now look like following:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d86c18dd-f1b6-4cc9-aef9-977e9bc9bc19" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">securityTokenIssuers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">  <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">add</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">key</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">CD29559E6EDC312272976AC43F7E921C5766D7063DAF6D177F3EEDEB1802FABE</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">encoding</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Hexadecimal</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">membershipProvider</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Default</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">realm</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://sts.pilesoft.com" rel="nofollow">http://sts.pilesoft.com</a></span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">/&gt;</span></li>
<li>      <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">add</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">key</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">6C4B865442D166796756C8DA1765584F7DD5EC0DE81B1CF29AC5FCE85AE5331D</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">encoding</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Hexadecimal</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">membershipProvider</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Default</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">realm</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://localhost" rel="nofollow">http://localhost</a></span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span></li>
<li style="background:#f3f3f3;">  <span style="background:#ffffff;color:#000000;"></span><span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">securityTokenIssuers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span></li>
<li><span style="background:#ffffff;color:#0000ff;"></span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#0000ff;"></span></li>
<li><span style="background:#ffffff;color:#0000ff;"></span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>In most cases, you need to configure a custom Membership provider as well, which I’m going to talk in a future post. </p>
<p><strong>Step 2: </strong>Open the main web.config file and locate the &lt;federatedAuthentication&gt; under the &lt;microsoft.identityModel&gt; section. This is WIF configuration and we need to change the &lt;wsFederation&gt; element to point to our custom STS. </p>
<p>Locate the &lt;wsFederation&gt; element &amp; change the <strong>issuer</strong> attribute to point to our Custom STS as shown below:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:044e9876-c17c-4d08-a669-403a744816ac" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#ffffff;max-height:300px;overflow:auto;padding:2px 5px;white-space:nowrap;"><span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">microsoft.identityModel</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />   <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">service</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">claimsAuthenticationManager</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">type</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Telerik.Sitefinity.Security.Claims.SFClaimsAuthenticationManager, Telerik.Sitefinity</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">securityTokenHandlers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />       <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">add</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">type</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Telerik.Sitefinity.Security.Claims.SWT.SWTSecurityTokenHandler, Telerik.Sitefinity</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">securityTokenHandlers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">audienceUris</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">mode</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Never</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">&gt;&lt;/</span><span style="background:#ffffff;color:#a31515;">audienceUris</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">federatedAuthentication</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />       <span style="background:#ffffff;color:#0000ff;"></span><br />     <span style="background:#ffffff;color:#000000;">==&gt;  </span><span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">wsFederation</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">passiveRedirectEnabled</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">true</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><br />                        <span style="background:#ffffff;color:#0000ff;"></span><span style="background:#ffffff;color:#ff0000;">issuer</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://sts.pilesoft.com/issue/sitefinity" rel="nofollow">http://sts.pilesoft.com/issue/sitefinity</a></span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">realm</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"><a href="http://localhost" rel="nofollow">http://localhost</a></span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">requireHttps</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">false</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />       <span style="background:#ffffff;color:#0000ff;"></span><br />       <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">cookieHandler</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">requireSsl</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">false</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">federatedAuthentication</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">issuerNameRegistry</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">type</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Telerik.Sitefinity.Security.Claims.CustomIssuerNameRegistry, Telerik.Sitefinity</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />       <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">trustedIssuers</span><span style="background:#ffffff;color:#0000ff;">&gt;&lt;/</span><span style="background:#ffffff;color:#a31515;">trustedIssuers</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">issuerNameRegistry</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br />     <span style="background:#ffffff;color:#0000ff;">&lt;</span><span style="background:#ffffff;color:#a31515;">issuerTokenResolver</span><span style="background:#ffffff;color:#0000ff;"> </span><span style="background:#ffffff;color:#ff0000;">type</span><span style="background:#ffffff;color:#0000ff;">=</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;">Telerik.Sitefinity.Security.Claims.SWT.WrapIssuerTokenResolver, Telerik.Sitefinity</span><span style="background:#ffffff;color:#000000;">&quot;</span><span style="background:#ffffff;color:#0000ff;"> /&gt;</span><br />   <span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">service</span><span style="background:#ffffff;color:#0000ff;">&gt;</span><br /> <span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">microsoft.identityModel</span><span style="background:#ffffff;color:#0000ff;">&gt;</span></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>Now if I browse to Sitefinity – I get:</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb.png?w=244&#038;h=153" width="244" height="153" /></a> </p>
<p>When I click on ‘Login to the backend link’, I’m redirected to my Custom STS. The address bar shows the sitefinity sign-in protocol in action. </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image1.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb1.png?w=244&#038;h=153" width="244" height="153" /></a></p>
<p>When I sign-in at the STS, it issues a SWT token &amp; redirects me back to the Sitefinity app. </p>
<p>As this STS is trusted by Sitefinity, it happily accepts the incoming SWT token and logs me in. </p>
<p><a href="http://zuahmed.files.wordpress.com/2013/02/image2.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/02/image_thumb2.png?w=244&#038;h=188" width="244" height="188" /></a> </p>
<p>I’ll publish the Custom STS code after removing the IP related bits. Ping me if you desperately needs it <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/438/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=438&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/02/06/custom-sts-for-sitefinity-5-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/02/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Enabling &#8216;Import Service Contract&#8217; menu option</title>
		<link>http://zamd.net/2013/01/02/enabling-import-service-contract-menu-option/</link>
		<comments>http://zamd.net/2013/01/02/enabling-import-service-contract-menu-option/#comments</comments>
		<pubDate>Wed, 02 Jan 2013 16:09:44 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[WF4.5]]></category>
		<category><![CDATA[Contract-First]]></category>
		<category><![CDATA[Import Service Contract]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=424</guid>
		<description><![CDATA[WF 4.5 introduced contact first development using which you can generate messaging activities from your existing WCF contracts. Out of box, this feature is only enabled for ‘WCF Workflow Service Application’ project type and is exposed using the ‘Import Service Contract’ context menu. This is quite useful feature and is certainly required in other project [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=424&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>WF 4.5 introduced contact first development using which you can generate messaging activities from your existing WCF contracts. Out of box, this feature is only enabled for ‘WCF Workflow Service Application’ project type and is exposed using the ‘Import Service Contract’ context menu.</p>
<p><a href="http://zuahmed.files.wordpress.com/2013/01/image.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/01/image_thumb.png?w=472&#038;h=180" width="472" height="180" /></a> <a href="http://zuahmed.files.wordpress.com/2013/01/image1.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2013/01/image_thumb1.png?w=244&#038;h=179" width="244" height="179" /></a> </p>
<p>This is quite useful feature and is certainly required in other project types as well. For example, a workflow hosted using WorkflowServiceHost in a windows service or a console application. You can easily enable the context menu option for other project types by including an additional GUID in the &lt;ProjectTypeGuids&gt; element in <strong>csproj</strong> file.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:326d3d30-e2b9-475b-a852-f47045f87fde" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background-color:#ffffff;overflow:auto;padding:2px 5px;"><span style="background:#ffffff;color:#0000ff;">  &lt;</span><span style="background:#ffffff;color:#a31515;">ProjectTypeGuids</span><span style="background:#ffffff;color:#0000ff;">&gt;{</span><span style="background:#ffffff;color:#000000;">32f31d43-81cc-4c15-9de6-3fc5453562b6};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</span><span style="background:#ffffff;color:#0000ff;">&lt;/</span><span style="background:#ffffff;color:#a31515;">ProjectTypeGuids</span><span style="background:#ffffff;color:#0000ff;">&gt;</span></div>
</p></div>
</p></div>
</p>
<ul>
<li>Unload the project in VS and open the csproj file using the xml editor. </li>
<li>Locate the &lt;ProjectTypeGuids&gt; element and insert this {349c5851-65df-11da-9384-00065b846f21} as the content of element along with other GUIDs. </li>
<li>Make sure to put a semicolon at the end of your newly inserted GUID. </li>
<li>Reload the project in VS and you should now see the ‘Import Service Contract’ menu option. </li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/424/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=424&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2013/01/02/enabling-import-service-contract-menu-option/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/01/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2013/01/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Service Bus Property Promotion Nuget Package</title>
		<link>http://zamd.net/2012/07/18/service-bus-property-promotion-nuget-package/</link>
		<comments>http://zamd.net/2012/07/18/service-bus-property-promotion-nuget-package/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 19:17:53 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=401</guid>
		<description><![CDATA[I have just published a Nuget package which adds property promotion features to Service Bus WCF programing model. Once you added the package to your project you can use the PromotedProperty attribute to mark your properties as promoted. The package supports promotion from both complex &#38; primitive arguments. In addition to PromotedPropertyAttribute you also need [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=401&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I have just published a <a href="https://nuget.org/packages/ServiceBus.Extensions.PromotedProperties/1.0.1">Nuget package</a> which adds <a href="http://zamd.net/2011/05/25/pubsub-with-wcf-part-2/">property promotion</a> features to <a href="http://www.microsoft.com/en-us/download/details.aspx?id=30376">Service Bus</a> WCF programing model. </p>
<p><a href="http://zuahmed.files.wordpress.com/2012/07/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://zuahmed.files.wordpress.com/2012/07/image_thumb.png?w=1028&#038;h=687" width="1028" height="687" /></a></p>
<p>Once you added the package to your project you can use the <strong>PromotedProperty </strong>attribute to mark your properties as promoted. The package supports promotion from both complex &amp; primitive arguments. In addition to PromotedPropertyAttribute you also need to stick <strong>PropertyPromotionBehavior</strong> on each method of your service contract. </p>
<p>Following service contract captures the sample usage. </p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0bc11f2f-720e-4e36-9a5b-c8bf2655216c" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Order</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">double</span> Amount { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</li>
<li style="background:#f3f3f3;">    [<span style="color:#2b91af;">PromotedProperty</span>]</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> ShipCity { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">[<span style="color:#2b91af;">ServiceContract</span>]</li>
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> <span style="color:#2b91af;">IOrderService</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    [<span style="color:#2b91af;">OperationContract</span>(Name = <span style="color:#a31515;">&quot;SubmitFlat&quot;</span>, IsOneWay = <span style="color:#0000ff;">true</span>)]</li>
<li style="background:#f3f3f3;">    [<span style="color:#2b91af;">PropertyPromotionBehavior</span>]</li>
<li>    <span style="color:#0000ff;">void</span> Submit(<span style="color:#0000ff;">double</span> amount, [<span style="color:#2b91af;">PromotedProperty</span>] <span style="color:#0000ff;">string</span> shipCity);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    [<span style="color:#2b91af;">OperationContract</span>(IsOneWay = <span style="color:#0000ff;">true</span>)]</li>
<li style="background:#f3f3f3;">    [<span style="color:#2b91af;">PropertyPromotionBehavior</span>]</li>
<li>    <span style="color:#0000ff;">void</span> Submit(<span style="color:#2b91af;">Order</span> order);</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>Enjoy… </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/401/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=401&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2012/07/18/service-bus-property-promotion-nuget-package/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/07/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Service Bus Server Install Experience</title>
		<link>http://zamd.net/2012/07/17/service-bus-server-install-experience/</link>
		<comments>http://zamd.net/2012/07/17/service-bus-server-install-experience/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 20:32:53 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=396</guid>
		<description><![CDATA[Today I installed Service Bus Server Beta release and the overall install experience was fairly smooth until I reached the New-SBFarm step of the ‘Getting started’ tutorial. The cmdlet just seems to hang for few minutes and failed ultimately – I tried on another machine &#38; got same results. After lot of head–scratching I narrowed [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=396&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today I installed <a href="http://www.microsoft.com/en-us/download/details.aspx?id=30376">Service Bus Server Beta</a> release and the overall install experience was fairly smooth until I reached the New-SBFarm step of the ‘<a href="http://msdn.microsoft.com/en-us/library/jj193021(v=azure.10)">Getting started’</a> tutorial. The cmdlet just seems to hang for few minutes and failed ultimately – I tried on another machine &amp; got same results. After lot of head–scratching I narrowed down the issue to SQL connectivity. Turns out New-SBFarm create 3 different databases, Farm management DB, Gateway DB &amp; the message container database. The first two DBs are created by the cmdlet itself &amp; it uses the Connection String passed into the cmdlet and just replaces the DB name. The message container DB creation is handled by another cmdlet ‘New-SBMessageContainer’ which uses the FQDN of the database server. </p>
<p>When server is identified using FQDN, SQL client code treats the connection as ‘Remote’ and because I was using a named instance – it tries to resolve the name using SQL Browser service which was by default disabled <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  </p>
<p>Hence the cmdlet hanged until connection request timeout – Enabling remote connections on Sql express &amp; starting the SQL Browser service has fixed the issue. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/396/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=396&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2012/07/17/service-bus-server-install-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>
	</item>
		<item>
		<title>Claim-based-security for ASP.NET Web APIs using DotNetOpenAuth</title>
		<link>http://zamd.net/2012/05/04/claim-based-security-for-asp-net-web-apis-using-dotnetopenauth/</link>
		<comments>http://zamd.net/2012/05/04/claim-based-security-for-asp-net-web-apis-using-dotnetopenauth/#comments</comments>
		<pubDate>Fri, 04 May 2012 18:50:11 +0000</pubDate>
		<dc:creator>zamd</dc:creator>
				<category><![CDATA[ASP.NET Web APIs]]></category>
		<category><![CDATA[ASP.NET Web API]]></category>

		<guid isPermaLink="false">https://zuahmed.wordpress.com/?p=378</guid>
		<description><![CDATA[Source Code Recently I worked with a customer assisting them in implementing their Web APIs using the new ASP.NET Web API framework. Their API would be public so obviously security came up as the key concern to address. Claims-Based-Security is widely used in SOAP/WS-* world and we have rich APIs available in .NET Framework in [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=378&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://sdrv.ms/JtDLO3">Source Code</a></p>
<p>Recently I worked with a customer assisting them in implementing their Web APIs using the new ASP.NET Web API framework. Their API would be public so obviously security came up as the key concern to address. Claims-Based-Security is widely used in SOAP/WS-* world and we have rich APIs available in .NET Framework in the form of WCF, WIF &amp; ADFS 2.0. Even though we now have this <a href="http://www.asp.net/web-api">cool library</a> to develop Web APIs, the claims-based-security story for REST/HTTP is still catching up. OAuth 2.0 is almost ready, OpenID Connect is catching up quickly however it would still take sometime before we have WIF equivalent libraries for implementing claims-based-security in REST/HTTP world. <a href="http://www.dotnetopenauth.net/">DotNetOpenAuth</a> seems to be the most prominent open-source library claiming to support OAuth 2.0 so I decided to give it a go to implement the <a href="http://tools.ietf.org/html/draft-ietf-oauth-v2-22#page-8">‘Resource Owner Password Credentials’</a> authorization grant. Following diagram shows the solution structure for my target scenario.</p>
<p><a href="http://zuahmed.files.wordpress.com/2012/05/clip_image002.png"><img style="background-image:none;margin:0;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="clip_image002" src="http://zuahmed.files.wordpress.com/2012/05/clip_image002_thumb.png?w=244&#038;h=101" alt="clip_image002" width="244" height="101" border="0" /></a></p>
<p>1. OAuth 2.0 issuer is an ASP.NET MVC application responsible for issuing token based on OAuth 2.0 ‘Password Credentials’ grant type.</p>
<p>2. Web API Host exposes secured Web APIs which can only be accessed by presenting a valid token issued by the trusted issuer</p>
<p>3. Sample thick client which consumes the Web API</p>
<p>I have used the <a href="http://nuget.org/packages/DotNetOpenAuth.Ultimate">DotNetOpenAuth.Ultimate</a> NuGet package which is just a single assembly implementing quite a few security protocols. From OAuth 2.0 perspective, AuthorizationServer is the main class responsible for processing the token issuance request, producing and returning a token for valid &amp; authenticated request. The token issuance action of my OAuthIssuerController looks like this:</p>
<h1>OAuth 2.0 Issuer</h1>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">OAuthIssuerController </span>: <span style="color:#2b91af;">Controller </span>{
    <span style="color:blue;">public </span><span style="color:#2b91af;">ActionResult </span>Index()
    {
        <span style="color:blue;">var </span>configuration = <span style="color:blue;">new </span><span style="color:#2b91af;">IssuerConfiguration </span>{
            EncryptionCertificate = <span style="color:blue;">new </span><span style="color:#2b91af;">X509Certificate2</span>(Server.MapPath(<span style="color:#a31515;">"~/Certs/localhost.cer"</span>)),
            SigningCertificate = <span style="color:blue;">new </span><span style="color:#2b91af;">X509Certificate2</span>(Server.MapPath(<span style="color:#a31515;">"~/Certs/localhost.pfx"</span>), <span style="color:#a31515;">"a"</span>)
        };

        <span style="color:blue;">var </span>authorizationServer = <span style="color:blue;">new </span><span style="color:#2b91af;">AuthorizationServer</span>(<span style="color:blue;">new </span><span style="color:#2b91af;">OAuth2Issuer</span>(configuration));
        <span style="color:blue;">var </span>response = authorizationServer.HandleTokenRequest(Request).AsActionResult();

        <span style="color:blue;">return </span>response;
    }
}</pre>
<p>AuthorizationServer handles all the protocol details and delegate the real token issuance logic to a custom token issuer handler (OAuth2Issuer in following snippet)</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4b7d6db0-2bc3-44fa-a893-23b067cc12de" class="wlWriterEditableSmartContent" style="margin:0;display:inline;float:none;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Protocol independent issuer</div>
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">OAuth2Issuer</span> : <span style="color:#2b91af;">IAuthorizationServer</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">IssuerConfiguration</span> _configuration;</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> OAuth2Issuer(<span style="color:#2b91af;">IssuerConfiguration</span> configuration)</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">if</span> (configuration == <span style="color:#0000ff;">null</span>) <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&#8220;configuration&#8221;</span>);</li>
<li style="background:#f3f3f3;">        _configuration = configuration;</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#2b91af;">RSACryptoServiceProvider</span> AccessTokenSigningKey</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">get</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">return</span> (<span style="color:#2b91af;">RSACryptoServiceProvider</span>)_configuration.SigningCertificate.PrivateKey;</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> DotNetOpenAuth.Messaging.Bindings.<span style="color:#2b91af;">ICryptoKeyStore</span> CryptoKeyStore</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">get</span> { <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">NotImplementedException</span>(); }</li>
<li style="background:#f3f3f3;">    }</li>
<li></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#2b91af;">TimeSpan</span> GetAccessTokenLifetime(DotNetOpenAuth.OAuth2.Messages.<span style="color:#2b91af;">IAccessTokenRequest</span> accessTokenRequestMessage)</li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">return</span> _configuration.TokenLifetime;</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#2b91af;">IClientDescription</span> GetClient(<span style="color:#0000ff;">string</span> clientIdentifier)</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">string</span> secretPassword = <span style="color:#a31515;">&#8220;test1243&#8243;</span>;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ClientDescription</span>(secretPassword, <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Uri</span>(<span style="color:#a31515;">&#8220;<a href="http://localhost/&#038;#8221" rel="nofollow">http://localhost/&#038;#8221</a>;</span>), <span style="color:#2b91af;">ClientType</span>.Confidential);</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#2b91af;">RSACryptoServiceProvider</span> GetResourceServerEncryptionKey(DotNetOpenAuth.OAuth2.Messages.<span style="color:#2b91af;">IAccessTokenRequest</span> accessTokenRequestMessage)</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">return</span> (<span style="color:#2b91af;">RSACryptoServiceProvider</span>)_configuration.EncryptionCertificate.PublicKey.Key;</li>
<li style="background:#f3f3f3;"></li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> IsAuthorizationValid(DotNetOpenAuth.OAuth2.ChannelElements.<span style="color:#2b91af;">IAuthorizationDescription</span> authorization)</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#008000;">//claims added to the token</span></li>
<li style="background:#f3f3f3;">        authorization.Scope.Add(<span style="color:#a31515;">&#8220;adminstrator&#8221;</span>);</li>
<li>        authorization.Scope.Add(<span style="color:#a31515;">&#8220;poweruser&#8221;</span>);</li>
<li style="background:#f3f3f3;"></li>
<li>        <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">    }</li>
<li></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> IsResourceOwnerCredentialValid(<span style="color:#0000ff;">string</span> userName, <span style="color:#0000ff;">string</span> password)</li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> DotNetOpenAuth.Messaging.Bindings.<span style="color:#2b91af;">INonceStore</span> VerificationCodeNonceStore</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">get</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">NotImplementedException</span>();</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol>
</div>
</div>
</div>
<p>Now with my issuer setup, I can acquire access tokens by POSTing following request to the token issuer endpoint</p>
<h1>Client</h1>
<p>POST /Issuer HTTP/1.1</p>
<p>Content-Type: application/x-www-form-urlencoded; charset=utf-8</p>
<p>scope=http%3A%2F%2Flocalhost%2F&amp;grant_type=client_credentials&amp;client_id=zamd&amp;client_secret=test1243</p>
<p>&nbsp;</p>
<p>In response, I get 200 OK with following payload</p>
<p>&nbsp;</p>
<p>HTTP/1.1 200 OK</p>
<p>Cache-Control: no-cache, no-store, max-age=0, must-revalidate</p>
<p>Pragma: no-cache</p>
<p>Content-Type: application/json; charset=utf-8</p>
<p>Server: Microsoft-IIS/7.5</p>
<p>Content-Length: 685</p>
<p>{&#8220;access_token&#8221;:&#8221;gAAAAC5KksmbH0FyG5snks_xOcROnIcPldpgksi5b8Egk7DmrRhbswiEYCX7RLdb2l0siW8ZWyqTqxOFxBCjthjTfAHrE8owe3hPxur7Wmn2LZciTYfTlKQZW6ujlhEv6N4V1HL4Md5hdtwy51_7RMzGG6MvvNbEU8_3GauIgaF7JcbQJAEAAIAAAABR4tbwLFF57frAdPyZsIeA6ljo_Y01u-2p5KTfJ2xa6ZhtEpzmC46Omcvps9MbFWgyz6536_77jx9nE3sePTSeyB5zyLznkGDKhjfWwx3KjbYnxCVCV-n2pqKtry0l8nkMj4MrjqoTXpvd_P0c_VGfVXCsVt7BYOO68QbD-m7Yz9rHIZn-CQ4po0FqS2elDVe9qwu_uATbAmOXlkWsbnFwa6_ZDHcSr2M-WZxHTVFin7vEWO7FxIQStabu_r4_0Mo_xaFlBKp2hl9Podq8ltx7KvhqFS0Xu8oIJGp1t5lQKoaJSRTgU8N8iEyQfCeU5hvynZVeoVPaXfMA-gyYfMGspLybaw7XaBOuFJ20-BZW0sAFGm_0sqNq7CLm7LibWNw&#8221;,&#8221;token_type&#8221;:&#8221;bearer&#8221;,&#8221;expires_in&#8221;:&#8221;300&#8243;,&#8221;scope&#8221;:&#8221;http:\/\/localhost\/ adminstrator poweruser&#8221;}</p>
<p><a href="http://zuahmed.files.wordpress.com/2012/05/image.png"><img style="background-image:none;margin:0;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://zuahmed.files.wordpress.com/2012/05/image_thumb.png?w=244&#038;h=190" alt="image" width="244" height="190" border="0" /></a></p>
<p>DotNetOpenAuth also has a WebServerClient class which can be used to acquire tokens and I have used in my test application instead of crafting raw HTTP requests. Following code snippet generates the same above request/response</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:60399082-8c96-46a3-b0ef-bfd6601b7d17" class="wlWriterEditableSmartContent" style="margin:0;display:inline;float:none;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Get Access Token</div>
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">IAuthorizationState</span> GetAccessToken()</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">var</span> authorizationServer = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">AuthorizationServerDescription</span></li>
<li style="background:#f3f3f3;">    {</li>
<li>        TokenEndpoint = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Uri</span>(<span style="color:#a31515;">&#8220;<a href="http://localhost:1960/Issuer&#038;#8221" rel="nofollow">http://localhost:1960/Issuer&#038;#8221</a>;</span>),</li>
<li style="background:#f3f3f3;">        ProtocolVersion = <span style="color:#2b91af;">ProtocolVersion</span>.V20</li>
<li></li>
<li style="background:#f3f3f3;">    };</li>
<li>    <span style="color:#0000ff;">var</span> client = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">WebServerClient</span>(authorizationServer, <span style="color:#a31515;">&#8220;<a href="http://localhost/&#038;#8221" rel="nofollow">http://localhost/&#038;#8221</a>;</span>);</li>
<li style="background:#f3f3f3;">    client.ClientIdentifier = <span style="color:#a31515;">&#8220;zamd&#8221;</span>;</li>
<li>    client.ClientSecret = <span style="color:#a31515;">&#8220;test1243&#8243;</span>;</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">var</span> state = client.GetClientAccessToken(<span style="color:#0000ff;">new</span>[] { <span style="color:#a31515;">&#8220;<a href="http://localhost/&#038;#8221" rel="nofollow">http://localhost/&#038;#8221</a>;</span> });</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">return</span> state;</li>
<li>}</li>
</ol>
</div>
</div>
</div>
<p>Ok Now the 2nd part is to use this access token for authentication &amp; authorization when consuming ASP.NET Web APIs.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d6553169-0048-4851-b708-f9473a13b1b3" class="wlWriterEditableSmartContent" style="margin:0;display:inline;float:none;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Web API Client</div>
<div style="background:#ddd;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;" start="1">
<li><span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">var</span> state = GetAccessToken();</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Expires = {0}&#8221;</span>, state.AccessTokenExpirationUtc);</li>
<li style="background:#f3f3f3;">    <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Token = {0}&#8221;</span>, state.AccessToken);</li>
<li></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">var</span> httpClient = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">OAuthHttpClient</span>(state.AccessToken)</li>
<li>    {</li>
<li style="background:#f3f3f3;">        BaseAddress = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Uri</span>(<span style="color:#a31515;">&#8220;<a href="http://localhost:2150/api/values&#038;#8221" rel="nofollow">http://localhost:2150/api/values&#038;#8221</a>;</span>)</li>
<li>    };</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Calling web api&#8230;&#8221;</span>);</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#2b91af;">Console</span>.WriteLine();</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">var</span> response = httpClient.GetAsync(<span style="color:#a31515;">&#8220;&#8221;</span>).Result;</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">if</span> (response.StatusCode==<span style="color:#2b91af;">HttpStatusCode</span>.OK)</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Console</span>.WriteLine(response.Content.ReadAsStringAsync().Result);</li>
<li>    <span style="color:#0000ff;">else</span></li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Console</span>.WriteLine(response);</li>
<li></li>
<li style="background:#f3f3f3;">    <span style="color:#2b91af;">Console</span>.ReadLine();</li>
<li>}</li>
</ol>
</div>
</div>
</div>
<p>On line 8, I’m creating an instance of a customized HttpClient passing in the access token. The httpClient would use this access token for all subsequent HTTP requests</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cb9ef1d3-b202-405a-b6ed-f3d6b9a70007" class="wlWriterEditableSmartContent" style="margin:0;display:inline;float:none;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">OAuth enabled HttpClient</div>
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">OAuthHttpClient</span> : <span style="color:#2b91af;">HttpClient</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">public</span> OAuthHttpClient(<span style="color:#0000ff;">string</span> accessToken)</li>
<li style="background:#f3f3f3;">        : <span style="color:#0000ff;">base</span>(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">OAuthTokenHandler</span>(accessToken))</li>
<li>    {</li>
<li style="background:#f3f3f3;"></li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">OAuthTokenHandler</span> : <span style="color:#2b91af;">MessageProcessingHandler</span></li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">string</span> _accessToken;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> OAuthTokenHandler(<span style="color:#0000ff;">string</span> accessToken)</li>
<li>            : <span style="color:#0000ff;">base</span>(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">HttpClientHandler</span>())</li>
<li style="background:#f3f3f3;">        {</li>
<li>            _accessToken = accessToken;</li>
<li style="background:#f3f3f3;"></li>
<li>        }</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#2b91af;">HttpRequestMessage</span> ProcessRequest(<span style="color:#2b91af;">HttpRequestMessage</span> request, System.Threading.<span style="color:#2b91af;">CancellationToken</span> cancellationToken)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            request.Headers.Authorization = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">AuthenticationHeaderValue</span>(<span style="color:#a31515;">&#8220;Bearer&#8221;</span>, _accessToken);</li>
<li>            <span style="color:#0000ff;">return</span> request;</li>
<li style="background:#f3f3f3;">        }</li>
<li></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#2b91af;">HttpResponseMessage</span> ProcessResponse(<span style="color:#2b91af;">HttpResponseMessage</span> response, System.Threading.<span style="color:#2b91af;">CancellationToken</span> cancellationToken)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">return</span> response;</li>
<li>        }</li>
<li style="background:#f3f3f3;">    }</li>
<li></li>
<li style="background:#f3f3f3;">}</li>
</ol>
</div>
</div>
</div>
<h1>Relying Party (ASP.NET Web APIs)</h1>
<p>Finally on the RP side, I have used standard MessageHandler extensibility to extract and validate the ‘access token’. The OAuth2 message handler also extracts the claims from the access token and create a ClaimsPrincipal which is passed on the Web API implementation for authorization decisions.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b021625e-3590-4f62-98d4-ff2b530d8e68" class="wlWriterEditableSmartContent" style="margin:0;display:inline;float:none;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">OAuth2 Message Handler</div>
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">OAuth2Handler</span> : <span style="color:#2b91af;">DelegatingHandler</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">ResourceServerConfiguration</span> _configuration;</li>
<li style="background:#f3f3f3;"></li>
<li>    <span style="color:#0000ff;">public</span> OAuth2Handler(<span style="color:#2b91af;">ResourceServerConfiguration</span> configuration)</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">if</span> (configuration == <span style="color:#0000ff;">null</span>) <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&#8220;configuration&#8221;</span>);</li>
<li style="background:#f3f3f3;">        _configuration = configuration;</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>    <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;">    {</li>
<li>        <span style="color:#2b91af;">HttpContextBase</span> httpContext;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">string</span> userName;</li>
<li>        <span style="color:#2b91af;">HashSet</span>&lt;<span style="color:#0000ff;">string</span>&gt; scope;</li>
<li style="background:#f3f3f3;"></li>
<li>        <span style="color:#0000ff;">if</span> (!request.TryGetHttpContext(<span style="color:#0000ff;">out</span> httpContext))</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">InvalidOperationException</span>(<span style="color:#a31515;">&#8220;HttpContext must not be null.&#8221;</span>);</li>
<li></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">var</span> resourceServer = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ResourceServer</span>(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">StandardAccessTokenAnalyzer</span>(</li>
<li>                                                    (<span style="color:#2b91af;">RSACryptoServiceProvider</span>)_configuration.IssuerSigningCertificate.PublicKey.Key,</li>
<li style="background:#f3f3f3;">                                                    (<span style="color:#2b91af;">RSACryptoServiceProvider</span>)_configuration.EncryptionVerificationCertificate.PrivateKey));</li>
<li></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">var</span> error = resourceServer.VerifyAccess(httpContext.Request, <span style="color:#0000ff;">out</span> userName, <span style="color:#0000ff;">out</span> scope);</li>
<li></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">if</span> (error != <span style="color:#0000ff;">null</span>)</li>
<li>            <span style="color:#0000ff;">return</span> <span style="color:#2b91af;">Task</span>&lt;<span style="color:#2b91af;">HttpResponseMessage</span>&gt;.Factory.StartNew(error.ToHttpResponseMessage);</li>
<li style="background:#f3f3f3;"></li>
<li>        <span style="color:#0000ff;">var</span> identity = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ClaimsIdentity</span>(scope.Select(s =&gt; <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Claim</span>(s, s)));</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrEmpty(userName))</li>
<li>            identity.Claims.Add(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Claim</span>(<span style="color:#2b91af;">ClaimTypes</span>.Name, userName));</li>
<li style="background:#f3f3f3;"></li>
<li>        httpContext.User = <span style="color:#2b91af;">ClaimsPrincipal</span>.CreateFromIdentity(identity);</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Thread</span>.CurrentPrincipal = httpContext.User;</li>
<li></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">base</span>.SendAsync(request, cancellationToken);</li>
<li>    }</li>
<li style="background:#f3f3f3;"></li>
<li>}</li>
</ol>
</div>
</div>
</div>
<p>Inside my Web API, I access the claims information using the standard IClaimsIdentity abstraction.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:46ff4e6a-4f75-47c1-b054-92a52f392330" class="wlWriterEditableSmartContent" style="margin:0;display:inline;float:none;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Accessing claims information</div>
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#0000ff;">string</span>&gt; Get()</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">if</span> (User.Identity.IsAuthenticated &amp;&amp; User.Identity <span style="color:#0000ff;">is</span> <span style="color:#2b91af;">IClaimsIdentity</span>)</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">return</span> ((<span style="color:#2b91af;">IClaimsIdentity</span>) User.Identity).Claims.Select(c =&gt; c.Value);</li>
<li>    <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">string</span>[] { <span style="color:#a31515;">&#8220;value1&#8243;</span>, <span style="color:#a31515;">&#8220;value2&#8243;</span> };</li>
<li style="background:#f3f3f3;">}</li>
</ol>
</div>
</div>
</div>
<h1>Fiddler Testing</h1>
<p>Once I got the “access token”, I can test few scenarios in fiddler by attaching and tweaking the token when calling my web api.</p>
<h5>401 without an “access token”</h5>
<p><a href="http://zuahmed.files.wordpress.com/2012/05/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://zuahmed.files.wordpress.com/2012/05/image_thumb1.png?w=532&#038;h=484" alt="image" width="532" height="484" border="0" /></a></p>
<h5>200 OK with a Valid token</h5>
<p><a href="http://zuahmed.files.wordpress.com/2012/05/image2.png"><img style="background-image:none;margin:0;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://zuahmed.files.wordpress.com/2012/05/image_thumb2.png?w=244&#038;h=190" alt="image" width="244" height="190" border="0" /></a></p>
<h5>401 with Expired token</h5>
<p><a href="http://zuahmed.files.wordpress.com/2012/05/image3.png"><img style="background-image:none;margin:0;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://zuahmed.files.wordpress.com/2012/05/image_thumb3.png?w=244&#038;h=146" alt="image" width="244" height="146" border="0" /></a></p>
<h5>401 with Tempered token</h5>
<p><a href="http://zuahmed.files.wordpress.com/2012/05/image4.png"><img style="background-image:none;margin:0;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://zuahmed.files.wordpress.com/2012/05/image_thumb4.png?w=244&#038;h=211" alt="image" width="244" height="211" border="0" /></a></p>
<p><a href="http://sdrv.ms/JtDLO3">Source code attached</a>. Please feel free to download and use.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zuahmed.wordpress.com/378/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zuahmed.wordpress.com/378/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zamd.net&#038;blog=13272925&#038;post=378&#038;subd=zuahmed&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zamd.net/2012/05/04/claim-based-security-for-asp-net-web-apis-using-dotnetopenauth/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d41c3b038d67898247bff3e462c42a5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zamd</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/05/clip_image002_thumb.png" medium="image">
			<media:title type="html">clip_image002</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://zuahmed.files.wordpress.com/2012/05/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
	</channel>
</rss>
