﻿<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://developers.de/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Armin Kalajdzija Posts : OWIN</title><link />http://developers.de/blogs/armin_kalajdzija/archive/tags/OWIN/default.aspx<description>Tags: OWIN</description><dc:language>en</dc:language><generator>CommunityServer 2008 SP1 (Build: 30619.63)</generator><item><title>How to secure Static Content under ASP.NET MVC and OWIN</title><link />http://developers.de/blogs/armin_kalajdzija/archive/2016/10/10/how-to-secure-static-content-under-asp-net-mvc-and-owin.aspx<pubdate>Mon, 10 Oct 2016 10:38:33 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:1800381</guid><dc:creator>Armin Kalajdzija</dc:creator><slash:comments>0</slash:comments><wfw:commentrss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://developers.de/blogs/armin_kalajdzija/rsscomments.aspx?PostID=1800381</wfw:commentrss><comments>http://developers.de/blogs/armin_kalajdzija/archive/2016/10/10/how-to-secure-static-content-under-asp-net-mvc-and-owin.aspx#comments</comments><description>&lt;p&gt;As you can see in my &lt;a href="http://developers.de/blogs/armin_kalajdzija/archive/2016/10/06/how-to-implement-web-sign-on-with-adfs-in-asp-net-mvc-using-owin.aspx"&gt;previous post&lt;/a&gt;, I am currently working on securing an ASP.NET MVC Web Application based on OWIN. Because of that is this post some sort of continuation of my previous one.  
&lt;/p&gt;&lt;p&gt;So, my Web Application in pure Single Page Application that works on AngularJS and is being generated by a Gulp Task Runner as a static content. Because we also have some REST Web API Methods in our Application that are used by Single Page Application, it was not possible to implement the Security of our Web Application completely on Client via ADAL or any other Javascript Library so we used (in our case Active Directory Federation Services) Owin Security Provider and added it to our ASP.NET MVC Web Application. Now we can easily use &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;background-color:white;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Tags on our Web API Methods.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;But now we had a Problem of securing our Single Page Application that is actually a static content in our ASP.NET MVC Web Application. For that I injected this simple Code Snippet that will be called upon every Request:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.Use((context, cont) =&amp;gt;
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; ((context.Authentication.User != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;) &amp;amp;&amp;amp;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;(context.Authentication.User.Identity != &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;) &amp;amp;&amp;amp;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;(context.Authentication.User.Identity.IsAuthenticated))
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;return&lt;span style="color:black;"&gt; cont();
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;else&lt;span style="color:black;"&gt;
			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;context.Authentication.Challenge(&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);&lt;br /&gt;&lt;span style="color:blue;"&gt;return&lt;span style="color:black;"&gt;
							&lt;span style="color:#2b91af;"&gt;Task&lt;span style="color:black;"&gt;.Delay(0);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});&lt;/span&gt;
	&lt;/p&gt;&lt;p&gt;It simply checks if the user is authenticated, and if not, it Challenges our OWIN Authentication Provider to authenticate the user (in our case ADFS). Now we don&amp;#39;t even have to use our &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;background-color:white;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Tags&lt;/span&gt; because all Request will need to be authenticated, what can make some problems in certain cases if you have some Web API Methods that needs to be publicly exposed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1800381" width="1" height="1"&gt;</description><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/Security/default.aspx">Security</category><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/OWIN/default.aspx">OWIN</category></item><item><title>How to implement Web Sign On with ADFS in ASP.NET MVC using OWIN</title><link />http://developers.de/blogs/armin_kalajdzija/archive/2016/10/06/how-to-implement-web-sign-on-with-adfs-in-asp-net-mvc-using-owin.aspx<pubdate>Thu, 06 Oct 2016 14:08:39 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:1800058</guid><dc:creator>Armin Kalajdzija</dc:creator><slash:comments>5</slash:comments><wfw:commentrss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://developers.de/blogs/armin_kalajdzija/rsscomments.aspx?PostID=1800058</wfw:commentrss><comments>http://developers.de/blogs/armin_kalajdzija/archive/2016/10/06/how-to-implement-web-sign-on-with-adfs-in-asp-net-mvc-using-owin.aspx#comments</comments><description>&lt;p&gt;In this Post I will (try to) shortly explain how to Implement Web Sign on with Active Directory Federation Services under ASP.NET MVC and OWIN/Katana as Middleware.
&lt;/p&gt;&lt;p&gt;There is plenty of Resources (read Code Snippets) on the Net about this subject, but what I actually found as important as the Code Snippets is actual Configuration of AD FS Server. The Configuration of ADFS is highly sensitive on every identifier, endpoint definition and even on every character like underscores, slashes, and of course prefixes like http/https are all exactly to be defined and used in your Web App!
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;ADFS Server Configuration&lt;/strong&gt;&lt;/span&gt;
	&lt;/p&gt;&lt;p&gt;That is why I will first show some screenshots and Configuration of my ADFS Server running on Azure VM Development Environment:
&lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/100616_5F00_1413_5F00_Howtoimplem1.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;Under &lt;strong&gt;Authentication Policies&lt;/strong&gt;, you should enable &lt;strong&gt;Forms Authentication&lt;/strong&gt; for &lt;strong&gt;Extranet&lt;/strong&gt; users. That way it will be possible for users outside your Domain or on a Public Computer to get a nice Forms Authentication provided by ADFS and still get authenticated.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;img src="http://developers.de/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/armin_5F00_kalajdzija/100616_5F00_1413_5F00_Howtoimplem2.png" alt="" /&gt;
	&lt;/p&gt;&lt;p&gt;You should create &lt;strong&gt;new Relying Party Trust&lt;/strong&gt; with following Configuration: &lt;strong&gt;Display Name&lt;/strong&gt; you can write whatever you want of course. Under &lt;strong&gt;Relaying party identifiers&lt;/strong&gt;, you should add exactly your Web Application URL (Including correct prefix and slash at the end. If you have more Bindings on you Web Application, define all of them. &lt;br /&gt;&lt;em&gt;NOTE: Some users had problems if their Web Application URLs contains underscores, so if you can, try to avoid them.
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Under &lt;strong&gt;Endpoint&lt;/strong&gt; Tab, add a &lt;strong&gt;WS-Federation Passive Endpoint&lt;/strong&gt; with the same URL of your Web Application as in &lt;strong&gt;Relying party identifiers&lt;/strong&gt;. &lt;br /&gt;&lt;em&gt;Note: You can also define multiple if you have more the one Binding, but only one can be Default. 
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Now knowing your exact ADFS Configuration, you will definitely save time and avoid configuration conditioned Problems so we can continue and finally write some Code.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;ASP.NET Web Application with OWIN Middleware
&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;First we will start off with OWIN &lt;strong&gt;Startup.cs&lt;/strong&gt; Class. We have the Authentication Configuration Method outsourced into separate Class called &lt;strong&gt;Startup.Auth.cs&lt;/strong&gt;, like the Default ASP.NET MVC Web Application Template do it, but many of you have it all directly under &lt;strong&gt;Startup.cs&lt;/strong&gt;.
&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Startup.cs
&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Threading.Tasks;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.WsFederation;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Owin;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:blue;"&gt;assembly&lt;span style="color:black;"&gt;: &lt;span style="color:#2b91af;"&gt;OwinStartupAttribute&lt;span style="color:black;"&gt;(&lt;span style="color:blue;"&gt;typeof&lt;span style="color:black;"&gt;(adfs.demo.&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;))]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;namespace&lt;span style="color:black;"&gt; adfs.demo
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;partial&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
								&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Configuration(&lt;span style="color:#2b91af;"&gt;IAppBuilder&lt;span style="color:black;"&gt; app)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;ConfigureAuth(app);
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Startup.Auth.cs
&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Configuration;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.Cookies;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Owin;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.WsFederation;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;namespace&lt;span style="color:black;"&gt; adfs.demo
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;partial&lt;span style="color:black;"&gt;
						&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
								&lt;span style="color:#2b91af;"&gt;Startup&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; ConfigureAuth(&lt;span style="color:#2b91af;"&gt;IAppBuilder&lt;span style="color:black;"&gt; app)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.UseCookieAuthentication(
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;CookieAuthenticationOptions&lt;span style="color:black;"&gt;
					&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;AuthenticationType = &lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.UseWsFederationAuthentication(
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationOptions&lt;span style="color:black;"&gt;
					&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;MetadataAddress = &lt;span style="color:#2b91af;"&gt;ConfigurationManager&lt;span style="color:black;"&gt;.AppSettings[&lt;span style="color:#a31515;"&gt;&amp;quot;ida:ADFSMetadata&amp;quot;&lt;span style="color:black;"&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:144pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;Wtrealm = &lt;span style="color:#2b91af;"&gt;ConfigurationManager&lt;span style="color:black;"&gt;.AppSettings[&lt;span style="color:#a31515;"&gt;&amp;quot;ida:Wtrealm&amp;quot;&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;});
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;app.SetDefaultSignInAsAuthenticationType(&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Here is important that you extract your &lt;strong&gt;ADFS Metadata Address&lt;/strong&gt; correctly from your Server which is by Default &lt;a href="http://youradfsserver.address.com/FederationMetadata/2007-06/FederationMetadata.xml"&gt;http://youradfsserver.address.com/FederationMetadata/2007-06/FederationMetadata.xml&lt;/a&gt;. 
&lt;/li&gt;&lt;li&gt;In Wtrealm out your &lt;strong&gt;Relying Party Identifier&lt;/strong&gt; from ADFS Server from first part of the Post. 
&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Both of this entries I have placed in Web.Config and it looks like this:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;&amp;lt;&lt;span style="color:#a31515;"&gt;appSettings&lt;span style="color:blue;"&gt;&amp;gt;&lt;span style="color:black;"&gt;
					&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;&amp;lt;&lt;span style="color:#a31515;"&gt;add&lt;span style="color:blue;"&gt;
					&lt;span style="color:red;"&gt;key&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;ida:ADFSMetadata&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;
											&lt;span style="color:red;"&gt;value&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;http://your-adfs-server.com/FederationMetadata/2007-06/FederationMetadata.xml&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt; /&amp;gt;&lt;span style="color:black;"&gt;
																	&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;&amp;lt;&lt;span style="color:#a31515;"&gt;add&lt;span style="color:blue;"&gt;
					&lt;span style="color:red;"&gt;key&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;ida:Wtrealm&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;
											&lt;span style="color:red;"&gt;value&lt;span style="color:blue;"&gt;=&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt;https://your-exact-site-url/&lt;span style="color:black;"&gt;&amp;quot;&lt;span style="color:blue;"&gt; /&amp;gt;&lt;span style="color:black;"&gt;
																	&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;&amp;lt;/&lt;span style="color:#a31515;"&gt;appSettings&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Now that should be enough to have OWIN Security running with our ADFS Server. All you have to do now is to add the &lt;span style="color:black;"&gt;&lt;span style="font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Tags on needed Controllers or &lt;span style="font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;AllowAnonymous&lt;span style="color:black;"&gt;] &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;on single Actions that needs to be accessed &lt;/span&gt;anonymously.
&lt;/p&gt;&lt;p&gt;Here is one very simple way of using a Templated &lt;strong&gt;AccountController.cs&lt;/strong&gt; with our ADFS Implementation:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Web;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; System.Web.Mvc;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.Cookies;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;using&lt;span style="color:black;"&gt; Microsoft.Owin.Security.WsFederation;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;namespace&lt;span style="color:black;"&gt; adfs.demo.Controllers
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;Authorize&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;class&lt;span style="color:black;"&gt;
						&lt;span style="color:#2b91af;"&gt;AccountController&lt;span style="color:black;"&gt; : &lt;span style="color:#2b91af;"&gt;Controller&lt;span style="color:black;"&gt;
									&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;[&lt;span style="color:#2b91af;"&gt;AllowAnonymous&lt;span style="color:black;"&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; Login(&lt;span style="color:blue;"&gt;string&lt;span style="color:black;"&gt; returnUrl)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;if&lt;span style="color:black;"&gt; (!Request.IsAuthenticated)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;HttpContext.GetOwinContext().Authentication.Challenge(&lt;span style="color:blue;"&gt;new&lt;span style="color:black;"&gt;
					&lt;span style="color:#2b91af;"&gt;AuthenticationProperties&lt;span style="color:black;"&gt; { RedirectUri = &lt;span style="color:#a31515;"&gt;&amp;quot;/adfs.demo&amp;quot;&lt;span style="color:black;"&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;
			&lt;span style="color:#2b91af;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;
 &lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;public&lt;span style="color:black;"&gt;
				&lt;span style="color:blue;"&gt;void&lt;span style="color:black;"&gt; LogOff()
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;{
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;string&lt;span style="color:black;"&gt; callbackUrl = Url.Action(&lt;span style="color:#a31515;"&gt;&amp;quot;Index&amp;quot;&lt;span style="color:black;"&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Home&amp;quot;&lt;span style="color:black;"&gt;, routeValues: &lt;span style="color:blue;"&gt;null&lt;span style="color:black;"&gt;, protocol: Request.Url.Scheme);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p style="margin-left:72pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;HttpContext.GetOwinContext().Authentication.SignOut(
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:blue;font-family:Consolas;font-size:9pt;"&gt;new&lt;span style="color:black;"&gt;
				&lt;span style="color:#2b91af;"&gt;AuthenticationProperties&lt;span style="color:black;"&gt; { RedirectUri = callbackUrl },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:108pt;"&gt;&lt;span style="color:#2b91af;font-family:Consolas;font-size:9pt;"&gt;WsFederationAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType, &lt;span style="color:#2b91af;"&gt;CookieAuthenticationDefaults&lt;span style="color:black;"&gt;.AuthenticationType);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left:36pt;"&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;}
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion
&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;It was actually pretty easy to implement the ADFS Authentication Provider in OWIN once you understand and have access the whole configuration of ADFS Server. So far it looks really fast and stable, beside few problems I had with Chrome and Firefox Browser that I will explain below. So as I promised, this was actually short post and I hope you will get your ADFS running smoothly just as I did. &lt;span style="color:black;font-family:Consolas;font-size:9pt;"&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chrome / Firefox Redirection Loop Problem&lt;br /&gt;&lt;/strong&gt;After my first Implementation of ADFS under OWIN, it looked like everything is working well. As soon as I tested it on Chrome and Firefox, I figured out that after Login, I am being redirected to ADFS Forms Authentication Web Page and back to my Default Return URL few times before getting Error Message (that was not telling me much about Error) on my ADFS Forms Authentication Web Page. &lt;br /&gt;&lt;br /&gt;In ADFS Event Log, there was this Error event:&lt;br /&gt;&lt;em&gt;Microsoft.IdentityServer.Web.InvalidRequestException: MSIS7042: The same client browser session has made &amp;#39;6&amp;#39; requests in the last &amp;#39;1&amp;#39; seconds. Contact your administrator for details.
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;There are many explanations and workarounds for the Redirection Loop Problem under OWIN Security, most of them have something to do with &lt;strong&gt;CookieManager&lt;/strong&gt; under OWIN but none of them has solved my problem. 
&lt;/p&gt;&lt;p&gt;My Solution:&lt;br /&gt;As soon as I have defined &amp;quot;fresh&amp;quot; new &lt;strong&gt;Relying Party Trust&lt;/strong&gt; (see first part of the Post) with exactly the same &lt;strong&gt;Relying Party Identifier&lt;/strong&gt; as an &lt;strong&gt;WS-Federation Passive Endpoint&lt;/strong&gt;
		&lt;strong&gt;URL&lt;/strong&gt; (and I really mean exactly the same, same &lt;strong&gt;prefix&lt;/strong&gt;, same &lt;strong&gt;URL&lt;/strong&gt; and &lt;strong&gt;/&lt;/strong&gt; at the end of URL, also see first part of the post) and then updated my &lt;strong&gt;Web.Config&lt;/strong&gt; with the exact Values, it all started working as a Charm.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1800058" width="1" height="1"&gt;</description><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/OWIN/default.aspx">OWIN</category><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/Active+Directory/default.aspx">Active Directory</category><category domain="http://developers.de/blogs/armin_kalajdzija/archive/tags/ADFS/default.aspx">ADFS</category></item></channel></rss>