it:ad:owin:howto:create_an_owin_authentication_middleware_component

IT:AD:OWin:HowTo:Create an OWin Authentication Middleware Component

Right…

Returning to our XXX component, notice that we didn't invoke the next module.

But we could have.

What's more, we could do something (ActionA), stop, call the next module, await its reponse, and then do ActionB.

If you think about it a sec, that basically achieves the same thing as HttpHandler offered.

The Cookie Middleware is the first handler – and last – pipeline module to work on the request.

We know this because the middleware components are processed in the order they are registered with OWin, in startup:

public void ConfigureAuth(IAppBuilder app) {
    ...
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider{
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    ...

Requests on the way in are analysed for a Application Session Cookie.

If found, a Claims Identity is created, and attached it to the current thread.

The next (the External Cookie Middleware) module is then invoked (we'll come back to that) and awaited.

When the next module(s) has completed, the Cookie Middleware has a final chance to do something. It looks at the environment for a WellKnown/ Security Environment value, the AuthenticationResponseGrant. (a AuthenticationResponseGrant will be in the Environment if the AuthenticationManager.SignIn was invoked). If it finds a AuthenticationResponseGrant, it creates a Session Cookie.

The cookie will be called AspNet.ExternalCookie.

User AgentUser AgentGoogleGoogleApplication Cookie MiddlewareApplication Cookie MiddlewareExternal Cookie MiddlewareExternal Cookie MiddlewareGoogle MiddlewareGoogle MiddlewareAuthControllerAuthControllerGoggle MiddlewareGoggle MiddlewareChallengeResultExternalLogin("Google", ["/Account/ExternalLoginCallback"])ChallengeResult.Execute()ChallengeResult.Execute()ChallengeResult.Executeinvokescontext.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider)which creates AuthenticationResponseChallengewithin the Owin environment.Check for Session Cookieopt[Found Cookie]Create Claims Identity

  • /home/skysigal/public_html/data/pages/it/ad/owin/howto/create_an_owin_authentication_middleware_component.txt
  • Last modified: 2023/11/04 01:52
  • by 127.0.0.1