只有在IsPersistent为True时,才会在写入Cookie指定Expires。需要注意的是浏览器中的Cookie过期时间仅仅是用来指定浏览器是否删除Cookie,而在Cookie存储的值中,也会包含该Cookie认证的发布时间和过期时间等,并在HandleAuthenticateAsync方法中对会其进行验证,并不是说只要你有Cookie就能验证通过。
源码解析 AddCookieAddCookie已多次用过,无需多说,直接看源码:
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder) => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, null, null); public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<CookieAuthenticationOptions> configureOptions) { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>()); return builder.AddScheme<CookieAuthenticationOptions, CookieAuthenticationHandler>(authenticationScheme, displayName, configureOptions); }其实现非常简单,首先注册了Cookie认证的配置项CookieAuthenticationOptions,而authenticationScheme参数用来指定当前认证的唯一的标识,不能重复。通常,使用默认的CookieAuthenticationDefaults.AuthenticationScheme就可以了,但是当我们同时使用多个Cookie认证方式时,需要手动为他们指定不同的Scheme。
最后,直接调用上一章中介绍的,完成对CookieAuthenticationHandler的注册。
CookieAuthenticationOptionsCookieAuthenticationOptions是针对Cookie认证的各种配置,如重定向地址,认证阶段事件的注册,Cookie名,过期时间等等,首先看一下它的定义:
public class CookieAuthenticationOptions : AuthenticationSchemeOptions { private CookieBuilder _cookieBuilder = new RequestPathBaseCookieBuilder { SameSite = SameSiteMode.Lax, HttpOnly = true, SecurePolicy = CookieSecurePolicy.SameAsRequest, }; public CookieAuthenticationOptions() { ExpireTimeSpan = TimeSpan.FromDays(14); ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter; SlidingExpiration = true; Events = new CookieAuthenticationEvents(); } public CookieBuilder Cookie { get => _cookieBuilder; set => _cookieBuilder = value ?? throw new ArgumentNullException(nameof(value)); } public new CookieAuthenticationEvents Events { get => (CookieAuthenticationEvents)base.Events; set => base.Events = value; } public ITicketStore SessionStore { get; set; } // 当用户未登录时,重定向到该路径,默认:/Account/Login public PathString LoginPath { get; set; } // 指定登出的路径,默认:/Account/Logout public PathString LogoutPath { get; set; } // 当用户无权访问时,重定向到该路径,默认:/Account/AccessDenied public PathString AccessDeniedPath { get; set; } // 返回地址参数名,默认:ReturnUrl public string ReturnUrlParameter { get; set; } // 指定Cookie的过期时间 public TimeSpan ExpireTimeSpan { get; set; } // 当Cookie过期时间已达一半时,是否重置为ExpireTimeSpan public bool SlidingExpiration { get; set; } // 用来将Cookie写入到浏览器或删除 public ICookieManager CookieManager { get; set; } public IDataProtectionProvider DataProtectionProvider { get; set; } public ISecureDataFormat<AuthenticationTicket> TicketDataFormat { get; set; } } CookieBuilder在 ASP.NET Core 2.0 中对针对Cookie的配置集中放到CookieBuilder类型当中,相比之前更加清晰:
public class CookieBuilder : object { public virtual string Name { get; set; } public virtual string Path { get; set; } public virtual string Domain { get; set; } public virtual bool HttpOnly { get; set; } public virtual SameSiteMode SameSite { get; set; } public virtual CookieSecurePolicy SecurePolicy { get; set; } public virtual TimeSpan? Expiration { get; set; } public virtual TimeSpan? MaxAge { get; set; } public CookieOptions Build(HttpContext context); }都是一些针对Cookie配置的标准用法,无需多说。
CookieAuthenticationEventsCookieAuthenticationEvents为我们提供了在Cookie认证的各个阶段(如,登录前后,退出前后,重定向等)注册事件的机会,以便我们拦截一些默认行为,来自定义处理逻辑。
public class CookieAuthenticationEvents { public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context) => OnValidatePrincipal(context); public virtual Task SigningIn(CookieSigningInContext context) => OnSigningIn(context); public virtual Task SignedIn(CookieSignedInContext context) => OnSignedIn(context); public virtual Task SigningOut(CookieSigningOutContext context) => OnSigningOut(context); public virtual Task RedirectToLogout(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToLogout(context); public virtual Task RedirectToLogin(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToLogin(context); public virtual Task RedirectToReturnUrl(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToReturnUrl(context); public virtual Task RedirectToAccessDenied(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToAccessDenied(context); }每一个事件都有它的默认实现,这里就不再多说,我们可以根据实际情况进行注册。
CookieAuthenticationHandler