<?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">1281</guid>
      <link>https://shazwazza.com/post/getting-umbraco-to-work-with-azure-easy-auth/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>Getting Umbraco to work with Azure Easy Auth</title>
      <description>&lt;p&gt;There’s a nifty feature in your Azure App Service that allows you to very quickly add authentication and authorization to your Azure website. You’ll see most folks calling this “Easy Auth” and there’s quite a few articles on the subject such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/tech-feed/azure-active-directorys-hidden-feature-easy-auth-315e34d92249" title="https://medium.com/tech-feed/azure-active-directorys-hidden-feature-easy-auth-315e34d92249"&gt;https://medium.com/tech-feed/azure-active-directorys-hidden-feature-easy-auth-315e34d92249&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://azure.microsoft.com/en-us/resources/videos/azure-websites-easy-authentication-and-authorization-with-chris-gillum/" title="https://azure.microsoft.com/en-us/resources/videos/azure-websites-easy-authentication-and-authorization-with-chris-gillum/"&gt;https://azure.microsoft.com/en-us/resources/videos/azure-websites-easy-authentication-and-authorization-with-chris-gillum/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-overview" title="https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-overview"&gt;https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The good news is this all works the way you’d expect with an Umbraco website for front-end requests but unfortunately this doesn’t play nicely with the Umbraco back office … but it’s easy to configure Umbraco to work!&lt;/p&gt;
&lt;h2&gt;The problem&lt;/h2&gt;
&lt;p&gt;The problem is that if you turn on Easy Auth and try to log in to your Umbraco back office, the login will succeed but you’ll get 401 responses for other back office requests and essentially you’ll see a blank screen. The reason this happens is due to the way that Easy Auth works:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It activates an HttpModule in your site called &lt;strong&gt;EasyAuthAspNetThreadPrincipalModule&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;During the &lt;em&gt;HttpModule.AuthenticateRequest&lt;/em&gt; stage it replaces the &lt;em&gt;Thread.CurrentPrincipal&lt;/em&gt; with it’s own &lt;em&gt;ClaimsPrincipal&lt;/em&gt;/&lt;em&gt;ClaimsIdentity&lt;/em&gt; instance&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Umbraco also sets the &lt;em&gt;Thread.CurrentPrincipal.Identity &lt;/em&gt;during this phase but at the OWIN level which executes before the &lt;em&gt;EasyAuthAspNetThreadPrincipalModule. &lt;/em&gt;Because the Easy Auth module replaces the principal/identity, it wipes out the one created by Umbraco. What it should do instead is check if the current principal is a &lt;em&gt;ClaimsPrincipal &lt;/em&gt;and then add it’s identity to the identity collection instead of wiping out anything that is already there. If that were the case, everything would ‘just work’ but since it is not we have to work around this issue.&lt;/p&gt;
&lt;h2&gt;The solution&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;UPDATE!&lt;/strong&gt; &lt;em&gt;(19/04/18) - &lt;a rel="noopener" href="https://twitter.com/cgillum" target="_blank" title="Chris Gillum"&gt;Chris Gillum&lt;/a&gt; who created Easy Auth got in touch with me on Twitter to share some &lt;a href="https://github.com/cgillum/easyauth/wiki"&gt;handy (and fairly hidden) documentation&lt;/a&gt; for Easy Auth. Looks like another work around is to use the &lt;a href="https://github.com/cgillum/easyauth/wiki/Advanced-Application-Settings"&gt;WEBSITE_AUTH_DISABLE_IDENTITY_FLOW&lt;/a&gt; appSetting which will prevent Easy Auth from setting the thread identity at all.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To work around this problem we need to tell Umbraco to perform it’s authentication procedure &lt;strong&gt;after&lt;/strong&gt; the Easy Auth module runs which is actually pretty easy to do.&lt;/p&gt;
&lt;p&gt;Create a new OWIN startup class:&lt;/p&gt;
&lt;pre class="lang-csharp"&gt;&lt;code&gt;
[assembly: OwinStartup("MyOwinStartup", typeof(MyOwinStartup))]
namespace MyProject
{
    public class MyOwinStartup : Umbraco.Web.UmbracoDefaultOwinStartup
    {
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Update an appSetting in your web.config to tell OWIN to use your class:&lt;/p&gt;
&lt;pre class="lang-xml"&gt;&lt;code&gt;
&amp;lt;add key="owin:appStartup" value="MyOwinStartup" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Override the method that configures Umbraco's authentication procedure and tell it to execute after the default Authentication stage, notice this code block is using PipelineStage.PostAuthenticate:&lt;/p&gt;
&lt;pre class="lang-csharp"&gt;&lt;code&gt;
public class MyOwinStartup : Umbraco.Web.UmbracoDefaultOwinStartup
{
    protected override void ConfigureUmbracoAuthentication(IAppBuilder app)
    {
        app
            .UseUmbracoBackOfficeCookieAuthentication(ApplicationContext, PipelineStage.PostAuthenticate)
            .UseUmbracoBackOfficeExternalCookieAuthentication(ApplicationContext, PipelineStage.PostAuthenticate)
            .UseUmbracoPreviewAuthentication(ApplicationContext, PipelineStage.Authorize);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That's it! Now the Umbraco back office will authenticate correctly with Azure Easy Auth turned on.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:56 Z</pubDate>
      <a10:updated>2023-03-23T15:08:56Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1297</guid>
      <link>https://shazwazza.com/post/easily-lock-out-umbraco-back-office-users/</link>
      <category>Umbraco</category>
      <title>Easily lock out Umbraco back office users</title>
      <description>&lt;p&gt;Want an easy way to lock out all back office users?&lt;/p&gt;
&lt;p&gt;Maybe you are performing an upgrade and want to make sure there’s no back office activity?&lt;/p&gt;
&lt;h2&gt;Here’s a handy script to do this&lt;/h2&gt;
&lt;pre class="lang-csharp"&gt;&lt;code&gt;
using System;
using Microsoft.Owin;
using Owin;
using Umbraco.Web;

[assembly: OwinStartup("AuthDisabledOwinStartup", typeof(MyWebsite.AuthDisabledOwinStartup))]

namespace MyWebsite
{
    public class AuthDisabledOwinStartup : UmbracoDefaultOwinStartup
    {
        protected override void ConfigureUmbracoAuthentication(IAppBuilder app)
        {
            //Don't do anything, this will mean all cookie authentication is disabled which means
            //that no requests from the back office user will be authenticated and therefore 
            //all requests will fail and the user will be logged out.
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now you can just update your web.config appSetting to&lt;/p&gt;
&lt;pre class="lang-xml"&gt;&lt;code&gt;
&amp;lt;add value="AuthDisabledOwinStartup" key="owin:appStartup"&amp;gt;&amp;lt;/add&amp;gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you want to allow back office access again, just update your web.config with your original &lt;em&gt;owin:appStartup&lt;/em&gt; value&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:52 Z</pubDate>
      <a10:updated>2023-03-23T15:08:52Z</a10:updated>
    </item>
    <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>
  </channel>
</rss>