In the Brock Allen blog , he claims to be
the CookieAuthenticationOptions class has a Provider property ... and it has properties that are delegates that you can subscribe to. This allows you to check the cookie as it enters the application (OnValidateIdentity). In this callback, you can reject or replace an identity.
I am new to OWIN and C #, so I try to adapt the many OnValidateIdentity examples I found on the Internet to suit my needs. After the cookie has been accepted as valid on every 'private' web page, I would like to check the following things:
- A cookie contains at least one requirement.
- CustomerId claim value is greater than zero
I can perform these two checks in a regular method, but I cannot figure out how to connect the login to OnValidateIdentity . Here is what I still have:
I wrote the code, but I canβt understand what needs to be returned from the method used.
public void Configuration(IAppBuilder app) { dynamic cookieExpirationPeriod = TimeSpan.FromMinutes(60); CookieAuthenticationProvider prov = new CookieAuthenticationProvider(); prov.OnValidateIdentity = ctx => { MyClaimsIdentityObject si = MyApp.Identity.Current(); if (si == null || si.UserId == 0 || si.CustomerId == 0) { ctx.RejectIdentity(); // what needs to happen here for a return value? } }; CookieAuthenticationOptions coa = new CookieAuthenticationOptions { AuthenticationMode = AuthenticationMode.Active, CookieName = "MyApp", ExpireTimeSpan = cookieExpirationPeriod, SlidingExpiration = true, AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/login.aspx"), CookieHttpOnly = true, Provider = prov }; if (HttpContext.Current.Request.IsLocal) { coa.CookieSecure = CookieSecureOption.Never; } else { coa.CookieSecure = CookieSecureOption.Always; } app.UseCookieAuthentication(coa); }
source share