配置web
在 Startup.ConfigureServices 方法中,建立具備 AddAuthentication 和 AddCookie 方法的身份驗證中間件服務:cookie
services.AddAuthentication(Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { // Cookie settings options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(20); options.LoginPath = "/Account/Login"; options.AccessDeniedPath = "/Account/AccessDenied"; options.SlidingExpiration = true; });
AuthenticationScheme 傳遞到 AddAuthentication 設置應用程序的默認身份驗證方案。 若是有多個 cookie 身份驗證明例,而且你想要使用特定方案進行受權,AuthenticationScheme 會頗有用。 將 AuthenticationScheme 設置爲CookieAuthenticationDefaults。 AuthenticationScheme爲方案提供值 "cookie"。 能夠提供任何用於區分方案的字符串值。
應用的身份驗證方案不一樣於應用的 cookie 身份驗證方案。 若是未向 AddCookie提供 cookie 身份驗證方案,則使用 CookieAuthenticationDefaults.AuthenticationScheme ("Cookie")。
默認狀況下,身份驗證 cookie 的 IsEssential 屬性設置爲 true。 當站點訪問者未贊成數據收集時,容許使用身份驗證 cookie。 session
在 Startup.Configure中,調用 UseAuthentication 和 UseAuthorization 設置 HttpContext.User 屬性,併爲請求運行受權中間件。 調用 UseEndpoints以前調用 UseAuthentication 和 UseAuthorization 方法:app
app.UseCookiePolicy();
app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapRazorPages(); });
登陸ide
若要建立保存用戶信息的 cookie,請構造一個 ClaimsPrincipal。 將對用戶信息進行序列化並將其存儲在 cookie 中。
使用任何所需的 Claim建立 ClaimsIdentity,並調用 SignInAsync 以登陸用戶:加密
var claims = new List<Claim> { new Claim(ClaimTypes.Name, user.Email), new Claim("FullName", user.FullName), new Claim(ClaimTypes.Role, "Administrator"), }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { //AllowRefresh = <bool>, // Refreshing the authentication session should be allowed. //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), // The time at which the authentication ticket expires. A // value set here overrides the ExpireTimeSpan option of // CookieAuthenticationOptions set with AddCookie. //IsPersistent = true, // Whether the authentication session is persisted across // multiple requests. When used with cookies, controls // whether the cookie's lifetime is absolute (matching the // lifetime of the authentication ticket) or session-based. //IssuedUtc = <DateTimeOffset>, // The time at which the authentication ticket was issued. //RedirectUri = <string> // The full path or absolute URI to be used as an http // redirect response value. }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
SignInAsync 建立加密的 cookie,並將其添加到當前響應中。 若是未指定 AuthenticationScheme,則使用默認方案。
ASP.NET Core 的數據保護系統用於加密。 對於託管在多臺計算機上的應用程序、跨應用程序或使用 web 場進行負載平衡,請將數據保護配置爲使用相同的密鑰環和應用程序標識符。spa
註銷code
若要註銷當前用戶並刪除其 cookie,請調用 SignOutAsync:中間件
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
若是 CookieAuthenticationDefaults.AuthenticationScheme (或 "Cookie")不用做方案(例如 "ContosoCookie"),請提供配置身份驗證提供程序時所使用的方案。 不然,將使用默認方案。blog