{"id":2693,"date":"2014-02-20T13:20:31","date_gmt":"2014-02-20T20:20:31","guid":{"rendered":"http:\/\/www.cloudidentity.com\/blog\/?p=2693"},"modified":"2014-02-21T02:07:43","modified_gmt":"2014-02-21T09:07:43","slug":"ws-federation-in-microsoft-owin-componentsa-quick-start","status":"publish","type":"post","link":"https:\/\/www.cloudidentity.com\/blog\/2014\/02\/20\/ws-federation-in-microsoft-owin-componentsa-quick-start\/","title":{"rendered":"WS-Federation in Microsoft OWIN Components&ndash;a quick start"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2014\/02\/21\/using-claims-in-your-web-app-is-easier-with-the-new-owin-security-components.aspx\">As you might have seen on the WebDev blog<\/a>, today we unveiled the first preview of the new WS-Federation support in Microsoft OWIN Components. In this quick post you\u2019ll see the new programming model in action. We will create a basic, no-frills app and configure it to authenticate users via WS-Federation and Windows Azure Active Directory. I will use Visual Studio 2013, but in fact you can just as well use VS 2012.<\/p>\n<h2>Create a basic MVC app<\/h2>\n<p>Let\u2019s start by creating a new project by selecting the ASP.NET web project template.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image24.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb24.png\" width=\"640\" height=\"447\" border=\"0\" \/><\/a><\/p>\n<p>We want to take over the authentication function, hence we want the project to start with no code for it. Press Change Authentication, choose No Authentication from the radio buttons on the left, and click OK until the project template is initialized.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image25.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb25.png\" width=\"640\" height=\"447\" border=\"0\" \/><\/a><\/p>\n<p>Before anything else, go to the HomeController and decorate with an[Authorize] attribute the HomeController class itself \u2013 this is for ensuring that no unauthenticated user can access it and for triggering the sign in flow once we\u2019ll have all the settings in place.<\/p>\n<p>Once you have done that, here\u2019s there\u2019s an extra step that is really good practice. We want the application to run on HTTPS. Select the project in Project Explorer, go in the project properties and flip the SSL Enabled property to True.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image26.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb26.png\" width=\"520\" height=\"480\" border=\"0\" \/><\/a><\/p>\n<p>IIS Express assigns the project a port. Select the https URL and copy it on the clipboard. Then, right-click on the project in Project Explorer and choose Properties from the menu.<\/p>\n<p>Pick the Web tab, locate the Project Url and paste in there the https URL. This will guarantee that when you debug your project starts on the correct address.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image27.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb27.png\" width=\"640\" height=\"447\" border=\"0\" \/><\/a><\/p>\n<p>Keep the URL in the clipboard, as you\u2019ll need it again soon!<\/p>\n<h2>Create the app entry in Windows Azure AD<\/h2>\n<p>In order to secure an application with Windows Azure AD, you need to provision an entry in your directory tenant that provides the app\u2019s coordinates.<\/p>\n<p>Navigate to <a title=\"https:\/\/manage.windowsazure.com\/\" href=\"https:\/\/manage.windowsazure.com\/\">https:\/\/manage.windowsazure.com\/<\/a> and sign in as an administrator. Locate the directory icon on the tab list on the left and click on it. Identify from the list the tenant you want to use and click on it. Finally, select the Applications header and choose Add from the bottom command bar. As we are developing a new app, choose the first option. That will start a short wizard that will help you to provision the application.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image28.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb28.png\" width=\"615\" height=\"463\" border=\"0\" \/><\/a><\/p>\n<p>Choose any name you like. I personally like to use the same name of the Visual Studio project, it makes it easier to find my way around when the number of apps grows, but you can use whatever technique works you like.<\/p>\n<p>Make sure that the Web application and\/or web API option is selected, then move to the next screen.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image29.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb29.png\" width=\"640\" height=\"438\" border=\"0\" \/><\/a><\/p>\n<p>This part is very important.<\/p>\n<p>The sign-on URL must match the URL of your application: you can paste here the address you\u2019ve been keeping in the clipboard.<\/p>\n<p>The App Id URI is the identifier for the application, or in WS-Federation parlance the realm of the application. You can choose any identifier that makes sense for you, as long as it is unique within the tenant. I usually try to follow a memorable schema. Remember, given that this is a URI this value does not need to correspond to any real network address!<\/p>\n<p>Once you have entered the values, move to the next screen.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image30.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb30.png\" width=\"640\" height=\"438\" border=\"0\" \/><\/a><\/p>\n<p>Here you decide on whata ccess rights your app has on the directory. FOr our purposes, you can keep the single sign-on value selected and finish the wizard.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image31.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb31.png\" width=\"640\" height=\"435\" border=\"0\" \/><\/a><\/p>\n<p>The next screen signals the successful conclusion of the app provisioning, and displays the two values you\u2019ll need to plug in your project in order to connect to Windows Azure AD.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image32.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb32.png\" width=\"594\" height=\"480\" border=\"0\" \/><\/a><\/p>\n<p>That\u2019s all you need to do on the portal. Switch back to Visual Studio, but keep the last page around: we\u2019ll need its values shortly.<\/p>\n<h2>Add the OWIN security components, add initialization logic<\/h2>\n<p>All of the tasks we have done so far are just business as usual. In this last section we are actually going to take advantage of the new system. With the current claims based model, at this point we would run a tool to generate the necessary configuration. With the new OWIN security components, we don\u2019t need any tool: coding is easy enough that we can do it manually.<\/p>\n<p>Let\u2019s start by adding references to the packages we need. Head to the paclkage manager console and enter the commands below:<\/p>\n<ul>\n<li><span style=\"font-family: Consolas; font-size: small;\">Install-Package Microsoft.Owin.Host.SystemWeb \u2013Version 3.0.0-alpha1 \u2013Pre<\/span><\/li>\n<li><span style=\"font-family: Consolas; font-size: small;\">Install-Package Microsoft.Owin.Security.WsFederation -Version 3.0.0-alpha1 \u2013Pre<\/span><\/li>\n<li><span style=\"font-family: Consolas; font-size: small;\">Install-Package Microsoft.Owin.Security.Cookies -Version 3.0.0-alpha1 \u2013Pre<\/span><\/li>\n<\/ul>\n<p>Those commands install, respectively: the hosting machinery (you need this for ensuring that the OWIN initialization gets picked up at launch time), the WS-Federation package (which chains all the other security components it needs) and the cookie middleware (for managing sessions).<\/p>\n<p>That done, we need to add the usual OWIN initialization files.<\/p>\n<p>Under the App_Start folder, add a new file Startup.Auth.cs.<\/p>\n<p>That done, change its content to look like the below (make sure you delete .App_Start from the namespace).<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">using<\/span> Microsoft.Owin.Security.Cookies;\r\n<span class=\"kwrd\">using<\/span> Microsoft.Owin.Security.WsFederation;\r\n<span class=\"kwrd\">using<\/span> Owin;\r\n\r\n<span class=\"kwrd\">namespace<\/span> WsFed_AAD1\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            app.UseCookieAuthentication(\r\n                <span class=\"kwrd\">new<\/span> CookieAuthenticationOptions\r\n                {\r\n                    AuthenticationType = \r\n                       WsFederationAuthenticationDefaults.AuthenticationType\r\n                });\r\n            app.UseWsFederationAuthentication(\r\n                <span class=\"kwrd\">new<\/span> WsFederationAuthenticationOptions\r\n                {\r\n                    MetadataAddress = <span class=\"str\">\"https:\/\/login.windows.net\/azurefridays.onmicrosoft.com\/federationmetadata\/2007-06\/federationmetadata.xml\"<\/span>,\r\n                    Wtrealm = <span class=\"str\">\"http:\/\/myapps\/WsFed_AAD1\"<\/span>,\r\n                });\r\n        }\r\n    }\r\n}<\/pre>\n<style type=\"text\/css\"><!--\n.csharpcode, .csharpcode pre\n{\nfont-size: small;\ncolor: black;\nfont-family: consolas, \"Courier New\", courier, monospace;\nbackground-color: #ffffff;\n\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt\n{\nbackground-color: #f4f4f4;\nwidth: 100%;\nmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }\n--><\/style>\n<p>This is largely boilerplate code. You only need to copy from the portal the values unique to your app and use them to initialize your wsFederationOptions. Namely: the metadata document URL in MetadataAddress, and the App ID URI in wtrealm. The WsFederationAuthentication middleware will use those for driving the sign in flow.<\/p>\n<p>The protocol specific part is already over! The last task we need to perform is simply to add one OWIN startup class. Visual Studio offers a shortcut for that, as shown below.<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image33.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb33.png\" width=\"479\" height=\"480\" border=\"0\" \/><\/a><\/p>\n<p>Once your file has been created, open it and modify it so that it looks like the one below. Pay attention to the partial keyboard, that\u2019s something I often forget.<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">using<\/span> System;\r\n<span class=\"kwrd\">using<\/span> System.Threading.Tasks;\r\n<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>(WsFed_AAD1.Startup))]\r\n<span class=\"kwrd\">namespace<\/span> WsFed_AAD1\r\n{\r\n    <span class=\"kwrd\">public 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<style type=\"text\/css\"><!--\n.csharpcode, .csharpcode pre\n{\nfont-size: small;\ncolor: black;\nfont-family: consolas, \"Courier New\", courier, monospace;\nbackground-color: #ffffff;\n\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt\n{\nbackground-color: #f4f4f4;\nwidth: 100%;\nmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }\n--><\/style>\n<p>Believe it or not, the project is now all set. We\u2019re ready to run it<\/p>\n<h2>Run the Project<\/h2>\n<p>Hit F5! Given that the entire controller is under [Authorize] you\u2019ll be redirected to the authentication experience right away. (If we would have used it only on specific actions, we would see this only when hitting one of those for the first time.)<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image34.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\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb34.png\" width=\"640\" height=\"422\" border=\"0\" \/><\/a><\/p>\n<p>Enter your credentials, hit enter and\u2026<\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image35.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\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb35.png\" width=\"640\" height=\"419\" border=\"0\" \/><\/a><\/p>\n<p>\u2026we\u2019re in! That was easy!<br \/>\nNote: If you\u2019ve been following along, you will not see in your app the text on the top right corner in the screenshot above. That\u2019s because I added just a couple of extra elements in the UI, that are super easy to add but require some cshtml and I didn\u2019t want to add too many instructions in this first tutorial. We\u2019ll dig much deeper in the next installments.<\/p>\n<h2>Deploy to Windows Azure<\/h2>\n<p>Deploying your app in Windows Azure is super easy: you can publish on a Windows Azure Web Site through the familiar Publish menu entry on the Project Explorer context menu.<\/p>\n<p>The only thing you need to ensure is that the URL of your web site is added to the app entry in Windows Azure AD. Navigate back to the management portal, select your directory, go in the applications list, select your app and choose Configure. Locate the Reply URL text list, and add in there the URL of your Windows Azure Web Site. Remember to use HTTPS! <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" alt=\"Smile\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/wlEmoticon-smile3.png\" \/><\/p>\n<p><a href=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image36.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\" alt=\"image\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/image_thumb36.png\" width=\"640\" height=\"162\" border=\"0\" \/><\/a><\/p>\n<p>Once you have done that, simply hit save. You can now run your app both from IIS express and on your web site.<\/p>\n<h2>Next<\/h2>\n<p>As clich\u00e9 as this might sound, we are barely scratching the surface here.<\/p>\n<p>The new components have the ambition to make all the easy scenarios easy to implement, while offering the same expressive power and fine grained control you came to expect form our claims based platform. In the next few days I\u2019ll discuss more in depth the programming surface and more advanced scenarios, such as<\/p>\n<ul>\n<li>Signing out<\/li>\n<li>Connecting to ADFS<\/li>\n<li>Mixing web UX and API<\/li>\n<li>Handling multi-tenant flows<\/li>\n<li>\u2026and anything else you\u2019ll express an interest in! <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" alt=\"Smile\" src=\"https:\/\/www.cloudidentity.com\/blog\/wp-content\/uploads\/2014\/02\/wlEmoticon-smile3.png\" \/><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As you might have seen on the WebDev blog, today we unveiled the first preview of the new WS-Federation support in Microsoft OWIN Components. In this quick post you\u2019ll see the new programming model in action. We will create a basic, no-frills app and configure it to authenticate users via WS-Federation and Windows&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2697,"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-2693","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\/2693","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=2693"}],"version-history":[{"count":2,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/posts\/2693\/revisions"}],"predecessor-version":[{"id":2696,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/posts\/2693\/revisions\/2696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/media\/2697"}],"wp:attachment":[{"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/media?parent=2693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/categories?post=2693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudidentity.com\/blog\/wp-json\/wp\/v2\/tags?post=2693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}