日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net core中使用cookie身份验证

發布時間:2023/12/4 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net core中使用cookie身份验证 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

ASP.NET Core Identity 是一個完整的全功能身份驗證提供程序,用于創建和維護登錄名。?但是, cookie 不能使用基于的身份驗證提供程序 ASP.NET Core Identity 。

配置

在 Startup.ConfigureServices 方法中,創建具有 AddAuthentication 和 AddCookie 方法的身份驗證中間件服務:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(); app.UseAuthentication();

AuthenticationScheme 傳遞到 AddAuthentication 設置應用程序的默認身份驗證方案。如果有多個 cookie 身份驗證實例,并且你想要使用特定方案進行授權,AuthenticationScheme 會很有用。將 AuthenticationScheme 設置為CookieAuthenticationDefaults。AuthenticationScheme為方案提供值 "cookie"。可以提供任何用于區分方案的字符串值。
應用的身份驗證方案不同于應用的 cookie 身份驗證方案。如果未向 AddCookie提供 cookie 身份驗證方案,則使用 CookieAuthenticationDefaults.AuthenticationScheme ("Cookie")。
默認情況下,身份驗證 cookie 的 IsEssential 屬性設置為 true。當站點訪問者未同意數據收集時,允許使用身份驗證 cookie。?

登錄

若要創建保存用戶信息的 cookie,請構造一個 ClaimsPrincipal。將對用戶信息進行序列化并將其存儲在 cookie 中。
使用任何所需的 Claim創建 ClaimsIdentity,并調用 SignInAsync 以登錄用戶:

/// <summary>////// </summary>/// <param name="model"></param>/// <param name="returnUrl"></param>/// <returns></returns>[HttpPost][AllowAttribute][ValidateAntiForgeryToken]public async Task<IActionResult> Login(LoginModel model, string returnUrl = null){if (!ModelState.IsValid){return Json(new { state = "error", message = "數據驗證失敗" });}string ip = GetRemoteIpAddress();var r = await UserApp.SaasLoginAsync(model.Account, model.Password, ip);if (!string.IsNullOrEmpty(r.Error)){return Json(new { state = "error", message = r.Error });}var claims = new List<Claim>{new Claim(ClaimTypes.UserData, getCurrentUser(r.User, ip).ToString()),};var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);var authProperties = new AuthenticationProperties{ExpiresUtc = DateTimeOffset.Now.AddMinutes(120)};await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(claimsIdentity),authProperties);return Json(new { state = "success", message = "登錄成功。", returnUrl = RedirectToLocal(returnUrl) });}

SignInAsync 創建加密的 cookie,并將其添加到當前響應中。如果未指定 AuthenticationScheme,則使用默認方案。
ASP.NET Core 的數據保護系統用于加密。對于托管在多臺計算機上的應用程序、跨應用程序或使用 web 場進行負載平衡,請將數據保護配置為使用相同的密鑰環和應用程序標識符。

注銷

?若要注銷當前用戶并刪除其 cookie,請調用 SignOutAsync:

/// <summary>////// </summary>/// <returns></returns>[HttpPost][ValidateAntiForgeryToken]public async Task<IActionResult> LogOff(){if (bool.Parse(Configuration.GetSection("IsIdentity").Value)){return SignOut("Cookies", "oidc");}else{if (User.Identity.IsAuthenticated){string userdata = User.Claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value;await UserApp.LogOffAsync(CurrentUser.FromJson(userdata));}await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);return RedirectToAction(actionName: nameof(Login), controllerName: "Account");}}

參考資料

https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/?view=aspnetcore-5.0

總結

以上是生活随笔為你收集整理的asp.net core中使用cookie身份验证的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。