<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>João Moreno</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/joo_moreno/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/joo_moreno/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 04:10:56 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>How to protect your MVC web app using ACS &amp; OAuth2 bearer tokens</title>
      <link>https://docs.microsoft.com/archive/blogs/joo_moreno/how-to-protect-your-mvc-web-app-using-acs-oauth2-bearer-tokens</link>
      <pubDate>Tue, 04 Mar 2014 02:44:00 GMT</pubDate>
      <dc:creator><![CDATA[João Moreno]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/joo_moreno/2014/03/04/how-to-protect-your-mvc-web-app-using-acs-oauth2-bearer-tokens/</guid>
      <description><![CDATA[Disclaimer
ACS &amp; HttpModules are no longer the de facto tools to achieve this sort of business....]]></description>
      <content:encoded><![CDATA[<h1>Disclaimer</h1>
<p><strong>ACS</strong> &amp; <strong>HttpModules</strong> are no longer the <em>de facto</em> tools to achieve this sort of business. Please refer to <a href="https://blogs.technet.com/b/ad/archive/2013/06/22/azure-active-directory-is-the-future-of-acs.aspx">AAD</a> and <a href="https://blogs.msdn.com/b/webdev/archive/2014/02/21/using-claims-in-your-web-app-is-easier-with-the-new-owin-security-components.aspx">OWIN Security Components</a> if you have the freedom to do so.</p>
<h1>TL;DR</h1>
<p>Securing your <a href="https://www.asp.net/mvc">MVC</a> application using <a href="https://msdn.microsoft.com/en-us/library/windowsazure/hh147631.aspx">ACS</a> is a simple task nowadays. The same doesn't apply for securing your REST endpoints with <a href="https://tools.ietf.org/html/rfc6749#section-7.1">OAuth2 bearer tokens</a>&nbsp;against third party applications, like command line tools. Using <a href="https://github.com/joaomoreno/AcsOAuth2">AcsOAuth2</a>, this post will guide you through configuring your app such that it can handle OAuth2 Bearer tokens - emitted by ACS - sent from the client via the <code>Authorization</code> HTTP header.</p>
<h1>Introduction</h1>
<p>We want to be able to authenticate requests between a command line application and an ACS protected web application. We will use OAuth2. This will be the overall flow:</p>
<ol>
<li>Given an authenticated user, the web application generates an <strong>authorization code</strong>, scoped to the user and a predefined identity.</li>
<li>A third party application can reach Azure ACS and use the predefined identity to exchange the authorization code for <strong>OAuth2 access and refresh tokens</strong>.</li>
<li>The third party application talks to the web application using the access token. It will be authenticated as the predefined identity, but carrying all the claims the original user's identity carried.</li>
</ol>
<h1>Create an application protected by ACS</h1>
<p>In order to create your app, I will ask you to follow the <a href="https://www.windowsazure.com/en-us/documentation/articles/active-directory-dotnet-how-to-use-access-control/" target="_blank">official guide</a>, which is very straightforward. What you should get out of it is an auth-capable web application.</p>
<p>Once you application is secure with ACS, follow the&nbsp;<a href="https://www.cloudidentity.com/blog/2012/11/20/introducing-the-developer-preview-of-the-json-web-token-handler-for-the-microsoft-net-framework-4-5-2/">Using the JWT Handler With WIF Applications</a> section of that <a href="https://www.cloudidentity.com/">Cloud Identity</a>&nbsp;blog post. It will guide you through configuring JWT tokens for the authentication process. After importing the service's certificate into <strong>Local Machine / Trusted People</strong>, please do it again into the <strong>Local Machine / Personal</strong> certificate store.</p>
<p>Now, if you try to make an HTTP request to it using a simple web client (e.g. <a href="https://curl.haxx.se/">curl</a>), you will notice the application will always respond with a <code>302</code> code, asking you to authenticate with whatever ACS namespace you have configured it. We want to be able to recognize requests that might have the&nbsp;<code>Authorization</code>&nbsp;HTTP header set with a valid OAuth2&nbsp;bearer token.</p>
<h1>Create ACS Service Identity</h1>
<p>We need to configure an identity on ACS which will serve as the service identity between your HTTP clients and your web application.</p>
<p>On the ACS management interface, under <strong>Service settings</strong>, click <strong>Service identities</strong>. Click <strong>Add</strong>. Pick a name for the service identity and click <strong>Generate</strong>&nbsp;to create a key for that identity. Click <strong>Save</strong>.</p>
<h1>Update Identity Redirect Address</h1>
<p>In order to enable ACS to emit OAuth2 access tokens for your app, you must configure the redirect address used for the previously created service identity.</p>
<p>First, create a new console application in Visual Studio. Add <a href="https://github.com/joaomoreno/AcsOAuth2">AcsOAuth2</a> as a NuGet dependency:&nbsp;<code>Install-Package AcsOAuth2 -Pre</code>. Then simply run the following code to set up the redirect address. The actual value is irrelevant, but remember it for you will need to use it later. As a rule of thumb, use the realm value you used before for your relying party application.</p>
<script type="text/javascript" src="https://gist.github.com/joaomoreno/9346967.js"></script>
<p>You need to do this only once, per app &amp; per service identity.</p>
<h1>Use the <code>JwtBearerAuthenticationModule</code></h1>
<p>Let's plug in the <code>JwtBearerAuthenticationModule</code> to your web application's request pipeline.</p>
<p>Just like the command line application, add&nbsp;<a href="https://github.com/joaomoreno/AcsOAuth2">AcsOAuth2</a>&nbsp;as a NuGet dependency:&nbsp;<code>Install-Package AcsOAuth2 -Pre</code>&nbsp;to your web application.</p>
<p>Then, open your web app's&nbsp;<strong>Global.asax.cs</strong> and make the following modifications:</p>
<script type="text/javascript" src="https://gist.github.com/joaomoreno/9347210.js"></script>
<p>You should match <strong>AUDIENCE_URI</strong> with the relying party's audience uri. <strong>ACS_NAMESPACE</strong> should simply be the name of your ACS namespace. You can find the value for&nbsp;<span><strong>CERTIFICATE_THUMBPRINT</strong> by going back to the ACS management portal, clicking on <strong>Certificates and keys</strong>, under <strong>Service settings</strong>, then <strong>Service Namespace (X.509 Certificate)</strong> and copying the thumbprint of the shown certificate.</span></p>
<h1>Generate Authorization Codes</h1>
<p>Your web application is now able to understand OAuth2 access tokens emitted by your ACS namespace. A third party application can get a hold of an <strong>access token</strong> by trading in an <strong>authorization token</strong> against ACS. Your web application can create authorization codes by using the <code>AcsOAuth2.anagementService</code> class:</p>
<script type="text/javascript" src="https://gist.github.com/joaomoreno/9347591.js"></script>
<p>The <strong>USERNAME</strong> value should be the <a href="https://msdn.microsoft.com/en-us/library/system.security.claims.claimsidentity.name(v=vs.110).aspx">Name </a>property on the ClaimsIdentity object of the authenticated user.</p>
<h1>Authenticating</h1>
<p>Once generated, an authorization code can be given to the third party application and used for the authentication process:</p>
<script type="text/javascript" src="https://gist.github.com/joaomoreno/9347693.js"></script>
<h1>References</h1>
<ul>
<li><a href="https://www.cloudidentity.com/blog/2012/11/20/introducing-the-developer-preview-of-the-json-web-token-handler-for-the-microsoft-net-framework-4-5-2/">Cloud Identity: Introducing the Developer Preview of the JSON Web Token Handler for the Microsoft .NET Framework 4.5</a></li>
<li><a href="https://msdn.microsoft.com/en-us/library/windowsazure/hh278947.aspx">ACS Management Service API Reference</a></li>
<li><a href="https://www.corradin.net/index.php/2013/01/invoking-the-acs-management-service-programmatically/">Invoking the ACS management service programmatically</a></li>
</ul>]]></content:encoded>
    </item>
  </channel>
</rss>