<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/0libq25y/frog.png?rmode=max&amp;v=1da0e911f4e6890</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1330</guid>
      <link>https://shazwazza.com/post/owin-cookie-authentication-with-variable-cookie-paths/</link>
      <category>ASP.Net</category>
      <title>OWIN Cookie Authentication with variable cookie paths</title>
      <description>&lt;p&gt;By default OWIN Cookie Authentication let’s you specify a single configurable cookie path that does not change for the lifetime of the application, for example&lt;/p&gt;
&lt;pre class="csharpcode"&gt;app.UseCookieAuthentication(&lt;span class="kwrd"&gt;new&lt;/span&gt; CookieAuthenticationOptions
{                
    CookiePath = &lt;span class="str"&gt;"/Client1"&lt;/span&gt;,
    CookieSecure = CookieSecureOption.SameAsRequest,
    CookieHttpOnly = &lt;span class="kwrd"&gt;true&lt;/span&gt;
});&lt;/pre&gt;
&lt;p&gt;This is going to only allow cookie authentication to occur when the request is for  any path under &lt;em&gt;/Client1. &lt;/em&gt;But what if you wanted this same cookie and cookie authentication provider to work for other variable paths, what if we wanted it to execute for multiple configured paths like: /Client1, /Client2/Secured, /Client3/Private ? Or what if we wanted wanted this Cookie Authentication Provider to execute dynamically based on the request object?&lt;/p&gt;
&lt;h2&gt;ICookieManager to the rescue&lt;/h2&gt;
&lt;p&gt;The &lt;em&gt;CookieAuthenticationOptions&lt;/em&gt; has a property: &lt;em&gt;CookieManager&lt;/em&gt; which you can set to any instance of &lt;em&gt;ICookieManager&lt;/em&gt;. &lt;em&gt;ICookieManager&lt;/em&gt; contains these methods: &lt;em&gt;GetRequestCookie&lt;/em&gt;, &lt;em&gt;AppendResponseCookie&lt;/em&gt;, &lt;em&gt;DeleteCookie&lt;/em&gt; but all we really need to worry about &lt;em&gt;GetRequestCookie&lt;/em&gt;. It turns out that the &lt;em&gt;CookieAuthenticationHandler&lt;/em&gt; will detect if this method returns null and if so it will just not continue trying to authenticate the request. To make things easy we’ll just inherit from the default OWIN &lt;em&gt;ICookieManager&lt;/em&gt; which is &lt;em&gt;ChunkingCookieManager&lt;/em&gt;, although it’s methods are not marked as virtual we can still override them by explicitly implementing the &lt;em&gt;ICookieManager.GetRequestCookie&lt;/em&gt; method (Pro Tip! You can always override a non virtual method if you explicitly implement an interfaces method).&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//explicit implementation&lt;/span&gt;
&lt;span class="kwrd"&gt;string&lt;/span&gt; ICookieManager.GetRequestCookie(IOwinContext context, &lt;span class="kwrd"&gt;string&lt;/span&gt; key)
{
    &lt;span class="rem"&gt;//TODO: Given what is in the context, we can check pretty much anything, if we don't want&lt;/span&gt;
    &lt;span class="rem"&gt;//this request to continue to be authenticated, just return null. Example:&lt;/span&gt;
    var toMatch = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] {&lt;span class="str"&gt;"/Client1"&lt;/span&gt;, &lt;span class="str"&gt;"/Client2/Secured"&lt;/span&gt;, &lt;span class="str"&gt;"/Client3/Private"&lt;/span&gt;};
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!toMatch.Any(m =&amp;gt; context.Request.Uri.AbsolutePath.StartsWith(m, StringComparison.OrdinalIgnoreCase)))
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    }

    &lt;span class="rem"&gt;//if we don't want to ignore it then continue as normal:&lt;/span&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;base&lt;/span&gt;.GetRequestCookie(context, key);            
}&lt;/pre&gt;
&lt;p&gt;The last step is to not worry about the &lt;em&gt;CookiePath&lt;/em&gt; since the custom &lt;em&gt;ICookieManager.GetRequestCookie&lt;/em&gt; is going to deal with whether the middleware receives a cookie value or not so in that case, the &lt;em&gt;CookiePath&lt;/em&gt; for the &lt;em&gt;CookieAuthenticationOptions&lt;/em&gt; will remain the default of &lt;em&gt;“/”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We’ve been doing this in Umbraco CMS core for quite some time, I meant to blog about this then but just didn’t find the time. In Umbraco we have a few custom request paths that we need authenticated with our custom back office cookie, for reference the &lt;em&gt;BackOfficeCookieManager&lt;/em&gt; source is found here: &lt;a href="https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/Security/Identity/BackOfficeCookieManager.cs" title="https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/Security/Identity/BackOfficeCookieManager.cs"&gt;https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/Security/Identity/BackOfficeCookieManager.cs&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:23 Z</pubDate>
      <a10:updated>2023-03-23T15:08:23Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1191</guid>
      <link>https://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-2/</link>
      <category>ASP.Net</category>
      <title>ASP.Net 5 Re-learning a few things (part 2)</title>
      <description>&lt;p&gt;This is part 2 of a series of posts about some fundamental changes in ASP.Net 5 that we’ll need to re-learn (or un-learn!)&lt;/p&gt; &lt;p&gt;Part 1: &lt;a title="http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/" href="http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/"&gt;http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/&lt;/a&gt;&lt;/p&gt; &lt;h2&gt;System.Web&lt;/h2&gt; &lt;p&gt;This probably isn’t new news to most people since it’s really one of the fundamental shifts for ASP.Net 5 – There won’t be a System.Web DLL. Everything that you’ll install in your website will come as different, smaller, separate libraries. For example, if you want to serve static files, you’d reference the &lt;em&gt;Microsoft.AspNet.StaticFiles&lt;/em&gt; package, if you want to use MVC, you’d include the &lt;em&gt;Microsoft.AspNet.Mvc &lt;/em&gt;package&lt;em&gt;.&lt;/em&gt; &lt;/p&gt; &lt;p&gt;ASP.Net 5: “&lt;a href="http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview" target="_blank"&gt;consists of modular components with minimal overhead, so you retain flexibility while constructing your solutions&lt;/a&gt;”&lt;/p&gt; &lt;h2&gt;Web Forms&lt;/h2&gt; &lt;p&gt;Gone! YAY! :-)&lt;/p&gt; &lt;p&gt;Web Forms will still be a part of .Net as part of the main framework in System.Web, just not part of ASP.Net 5. &lt;/p&gt; &lt;h2&gt;HttpModules &lt;/h2&gt; &lt;p&gt;An HttpModule simply doesn’t exist in Asp.Net 5, but of course there is a new/better way to achieve this functionality, it’s called Middleware. In an HttpModule, you had to execute code based on the various &lt;a href="http://msdn.microsoft.com/en-us/library/bb470252.aspx" target="_blank"&gt;stages&lt;/a&gt; of a request, things such as &lt;em&gt;AuthenticateRequest, AuthorizeRequest, PostResolveRequestCache&lt;/em&gt;, and other slightly confusingly named events. This is no longer the case with Middleware, things just make sense now … everything is simply a linear execution of your code. You can have multiple middleware’s defined to execute in your application and each one is registered explicitly in your Startup.cs file. As a developer, you are in full control of what get’s executed and in what order instead of not knowing which HttpModules are executing and not really in control of their order of execution. Middleware can simply modify a request and continue calling the next one in the chain, or it can just terminate the pipeline and return a result. &lt;/p&gt; &lt;p&gt;There’s loads of examples in the source for middleware, ranging from the &lt;a href="https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs" target="_blank"&gt;static file middleware&lt;/a&gt; to &lt;a href="https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs" target="_blank"&gt;cookie authentication middleware&lt;/a&gt;,&amp;nbsp; etc…&lt;/p&gt; &lt;p&gt;And &lt;a href="http://whereslou.com/2014/05/28/asp-net-moving-parts-ibuilder/" target="_blank"&gt;here’s a good article&lt;/a&gt; that explains middeware registration and the flow of control.&lt;/p&gt; &lt;h2&gt;HttpHandlers&lt;/h2&gt; &lt;p&gt;HttpHandlers are also a thing of the past. All they really were was a request handler that was based on a specific request path. MVC (which now also includes WebApi) has got this covered. If you really wanted, you could create middleware for this type of functionality as well but unless you require something extraordinarily complex that MVC cannot do (and it can do a lot!), I’d recommend just sticking with MVC.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
  </channel>
</rss>