{"id":2931,"date":"2014-07-28T23:34:50","date_gmt":"2014-07-29T06:34:50","guid":{"rendered":"http:\/\/www.cloudidentity.com\/blog\/?p=2931"},"modified":"2014-07-28T23:40:06","modified_gmt":"2014-07-29T06:40:06","slug":"protecting-an-mvc4-vs2012-project-with-openid-connect-and-azure-ad","status":"publish","type":"post","link":"https:\/\/www.cloudidentity.com\/blog\/2014\/07\/28\/protecting-an-mvc4-vs2012-project-with-openid-connect-and-azure-ad\/","title":{"rendered":"Protecting an MVC4 VS2012 Project with OpenId Connect and Azure AD"},"content":{"rendered":"<p>I have to say I am pretty surprised by the attention that last week\u2019s OIDC OWIN+WebForms post has garnered. Had I known, I would have posted about it much earlier!<\/p>\n<p>In the same spirit, here there\u2019s another quick tutorial addressing a common FAQ: \u201cMy company is still on VS2012: can I use the OpenId Connect\/WS-Fed middleware?\u201d<\/p>\n<p>The answer is \u201cAs long as you target .NET 4.5, totally!\u201d <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/wlEmoticon-smile6.png\" alt=\"Smile\" \/><\/p>\n<p>Just make things a tad more actionable, here there are some basic instructions on how to make a minimal MVC4 app work with AAD and OpenId Connect.<\/p>\n<h2>Create an empty project<\/h2>\n<p>Fire up the (even more) good ol\u2019 VS2012, create a new project and navigate through the templates until you get to Visual C#\/Web\/ASP.NET MVC 4 Web Application. Choose a name and click OK.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image_thumb9.png\" alt=\"image\" width=\"640\" height=\"363\" border=\"0\" \/><\/a><\/p>\n<p>Here you have a variety of choices. My personal preference is \u201cintranet application\u201d, mostly because it is the project type with the least amount of stuff I don\u2019t need \u2013 hence I spend less tile deleting stuff after creation.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image_thumb10.png\" alt=\"image\" width=\"530\" height=\"480\" border=\"0\" \/><\/a><\/p>\n<p>We\u2019ll be doing things by the book, hence we\u2019ll enable SSL. The flow was already pretty much the same one you find in VS2013:<\/p>\n<ul>\n<li>Select the project node in solution explorer. Hit F4.<\/li>\n<li>In the resulting property pages, flip SSL Enabled from false to true.<\/li>\n<li>Copy the newly populated SSL URL.<\/li>\n<li>Right click on the project node. Choose properties.<\/li>\n<li>Move to the Web tab. Paste the SSL Url in the Project Url field. Shift+CTR+S to save the new settings.<\/li>\n<\/ul>\n<p>Here we do need to change a couple of things from the template, mostly to disable its Windows auth settings. Open the web config and:<\/p>\n<ul>\n<li>Change the &lt;authentication&gt; element by setting its mode attribute to \u201cNone\u201d\\<\/li>\n<li>Locate the &lt;authorization&gt; element and delete it.<\/li>\n<\/ul>\n<p>That\u2019s it! Next: configure your app in AAD.<\/p>\n<h2>Provision the app in Azure AD<\/h2>\n<p>This is exactly the same task for any platform, hence the indications I gave for VS2013 apply verbatim here as well.<\/p>\n<p>Navigate to <a href=\"https:\/\/manage.windowsazure.com\/\">https:\/\/manage.windowsazure.com\/<\/a>, sign in as your tenant admin, scroll to the Active Directory tab, choose the tenant you want to use, select the Applications tab, and click the Add button on the appbar at the bottom of the screen.<\/p>\n<p>Choose \u201cAdd an application my organization is developing\u201d.<\/p>\n<p>Give to the app any name you like. Keep the default \u201cweb application and\/or web api\u201d. Click the Next arrow.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image11.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image_thumb11.png\" alt=\"image\" width=\"373\" height=\"267\" border=\"0\" \/><\/a><\/p>\n<p>In the Sign-On URL enter the HTTPS address you got when you enabled SSL on the project (mine is <a href=\"https:\/\/localhost:44307\/\">https:\/\/localhost:44307\/<\/a>). In the App ID URI enter any valid URI that will later remind you of what this app is. For my test app I chose <a href=\"http:\/\/OldFashionOWINisAwesomeS\">http:\/\/OldFashionOWINisAwesomeS<\/a>. Click the Done button.<\/p>\n<p>Click on the Configure tab and leave the browser open there. We\u2019re going to need some of the values here in just a moment.<\/p>\n<h2>Add references to the Cookie\/OpenId Connect\/SystemWeb NuGets<\/h2>\n<p>Once again, exactly the same deal as VS2013. Let\u2019s go back to Visual Studio. Go to Tools-&gt;Library Package Manager-&gt;Package Manager Console. In the console, enter the following three magic commands:<\/p>\n<p><span style=\"font-family: Consolas;\">Install-Package Microsoft.Owin.Security.OpenIdConnect -Pre<\/span><\/p>\n<p><span style=\"font-family: Consolas;\">Install-Package Microsoft.Owin.Security.Cookies \u2013Pre<\/span><\/p>\n<p><span style=\"font-family: Consolas;\">Install-Package Microsoft.Owin.Host.SystemWeb \u2013Pre<\/span><\/p>\n<p>That will install the necessary OWIN packages.<\/p>\n<h2>Add the initialization logic<\/h2>\n<p>Here we really need to enable the OWIN pipeline forms cratch, given that the tempalte we used is 100% unaware of it. Luckily, it\u2019s a trivial task.<\/p>\n<p>In the root of the project, add a new class. Name the file Startup.cs.<\/p>\n<p>Here there\u2019s the code you want to have in there:<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">using<\/span> Microsoft.Owin;\r\n<span class=\"kwrd\">using<\/span> Owin;\r\n\r\n[assembly: OwinStartup(<span class=\"kwrd\">typeof<\/span>(OldFashionOWIN.Startup))]\r\n\r\n<span class=\"kwrd\">namespace<\/span> OldFashionOWIN\r\n{\r\n    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">partial<\/span> <span class=\"kwrd\">class<\/span> Startup\r\n    {\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> Configuration(IAppBuilder app)\r\n        {\r\n            ConfigureAuth(app);\r\n        }\r\n    }\r\n}<\/pre>\n<p>This class\u2019 implementation of the Configuration method\u00a0 will be automatically called upon the first request comes in. All it does is invoking ConfigureAuth, the authentication initialization logic which we will add in another file. HEad to the App_startup folder, add a new class Startup.Auth.cs, and make its code look like the following:<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">using<\/span> Microsoft.Owin.Security;\r\n<span class=\"kwrd\">using<\/span> Microsoft.Owin.Security.Cookies;\r\n<span class=\"kwrd\">using<\/span> Microsoft.Owin.Security.OpenIdConnect;\r\n<span class=\"kwrd\">using<\/span> Owin;\r\n\r\n<span class=\"kwrd\">namespace<\/span> OldFashionOWIN\r\n{\r\n    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">partial<\/span> <span class=\"kwrd\">class<\/span> Startup\r\n    {\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> ConfigureAuth(IAppBuilder app)\r\n        {\r\n\r\n            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);\r\n            app.UseCookieAuthentication(<span class=\"kwrd\">new<\/span> CookieAuthenticationOptions());\r\n            app.UseOpenIdConnectAuthentication(\r\n                <span class=\"kwrd\">new<\/span> OpenIdConnectAuthenticationOptions\r\n                {\r\n                    ClientId = <span class=\"str\">\"475d1913-d6e7-422c-8dbc-3a94ed21cfaa\"<\/span>,\r\n                    Authority = <span class=\"str\">\"https:\/\/login.windows.net\/developertenant.onmicrosoft.com\"<\/span>                    \r\n                });\r\n        }\r\n    }\r\n}<\/pre>\n<p>The Authority part indicates the AAD tenant you want to use, while you can get the value of ClientId from the Configure tab of the application entry in the Azure portal, which should still be available in the browser from few steps earlier in the tutorial.<br \/>\nI already explained in various places what that middleware does, hence I won\u2019t bore you again about it here.<br \/>\nNote: the namespace (VS will append a .App_start, delete it) and the fact that the class is partial.<\/p>\n<p>Finally: head to the Controllers\/HomeController.cs file and decorate the class with the following:<\/p>\n<pre class=\"csharpcode\">[Authorize]\r\n<span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> HomeController : Controller\r\n{\r\n    <span class=\"rem\">\/\/...<\/span>\r\n<\/pre>\n<p>That will tell ASP.NET that all requestors of this controller\u2019s actions must be authenticated, hence it will help to trigger the auth experience at start up time.<\/p>\n<h2>Give it a spin!<\/h2>\n<p>Hit F5. You\u2019ll be bounced right away to AAD, Sign in with your user of choice.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image12.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image_thumb12.png\" alt=\"image\" width=\"593\" height=\"480\" border=\"0\" \/><\/a><\/p>\n<p>Et voila! You are treated with the cerulean theme that was in fashion back in VS2012 times, and the upper right corner shows that you successfully signed in. Pretty easy!<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image13.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/image_thumb13.png\" alt=\"image\" width=\"593\" height=\"480\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>As I hope the above has shown, the OWIN model is super flexible and can be applied pretty much anywhere there\u2019s .NET 4.5 available. That means that if for one reason or another you didn\u2019t upgrade to VS2013, you can still take full advantage of Azure AD, ADFS and the ease of use of the new OWIN based middlewares <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/07\/wlEmoticon-smile6.png\" alt=\"Smile\" \/> have fun!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have to say I am pretty surprised by the attention that last week\u2019s OIDC OWIN+WebForms post has garnered. Had I known, I would have posted about it much earlier! In the same spirit, here there\u2019s another quick tutorial addressing a common FAQ: \u201cMy company is still on VS2012: can I use the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2930,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/posts\/2931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/comments?post=2931"}],"version-history":[{"count":2,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/posts\/2931\/revisions"}],"predecessor-version":[{"id":2933,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/posts\/2931\/revisions\/2933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/media\/2930"}],"wp:attachment":[{"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/media?parent=2931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/categories?post=2931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/tags?post=2931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}