自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Asp.net core中使用cookie身份驗(yàn)證

安全 應(yīng)用安全
ASP.NET Core Identity 是一個完整的全功能身份驗(yàn)證提供程序,用于創(chuàng)建和維護(hù)登錄名。 但是, cookie 不能使用基于的身份驗(yàn)證提供程序 ASP.NET Core Identity 。

[[381956]]

本文轉(zhuǎn)載自微信公眾號「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請聯(lián)系UP技術(shù)控公眾號。conan5566  

背景

ASP.NET Core Identity 是一個完整的全功能身份驗(yàn)證提供程序,用于創(chuàng)建和維護(hù)登錄名。 但是, cookie 不能使用基于的身份驗(yàn)證提供程序 ASP.NET Core Identity 。

配置

在 Startup.ConfigureServices 方法中,創(chuàng)建具有 AddAuthentication 和 AddCookie 方法的身份驗(yàn)證中間件服務(wù):

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

AuthenticationScheme 傳遞到 AddAuthentication 設(shè)置應(yīng)用程序的默認(rèn)身份驗(yàn)證方案。如果有多個 cookie 身份驗(yàn)證實(shí)例,并且你想要使用特定方案進(jìn)行授權(quán),AuthenticationScheme 會很有用。將 AuthenticationScheme 設(shè)置為CookieAuthenticationDefaults。AuthenticationScheme為方案提供值 "cookie"??梢蕴峁┤魏斡糜趨^(qū)分方案的字符串值。

應(yīng)用的身份驗(yàn)證方案不同于應(yīng)用的 cookie 身份驗(yàn)證方案。如果未向 AddCookie提供 cookie 身份驗(yàn)證方案,則使用 CookieAuthenticationDefaults.AuthenticationScheme ("Cookie")。

默認(rèn)情況下,身份驗(yàn)證 cookie 的 IsEssential 屬性設(shè)置為 true。當(dāng)站點(diǎn)訪問者未同意數(shù)據(jù)收集時,允許使用身份驗(yàn)證 cookie。

登錄

若要創(chuàng)建保存用戶信息的 cookie,請構(gòu)造一個 ClaimsPrincipal。將對用戶信息進(jìn)行序列化并將其存儲在 cookie 中。

使用任何所需的 Claim創(chuàng)建 ClaimsIdentity,并調(diào)用 SignInAsync 以登錄用戶:

  1. /// <summary> 
  2.         /// 
  3.         /// </summary> 
  4.         /// <param name="model"></param> 
  5.         /// <param name="returnUrl"></param> 
  6.         /// <returns></returns
  7.         [HttpPost] 
  8.         [AllowAttribute] 
  9.         [ValidateAntiForgeryToken] 
  10.         public async Task<IActionResult> Login(LoginModel model, string returnUrl = null
  11.         { 
  12.             if (!ModelState.IsValid) 
  13.             { 
  14.                 return Json(new { state = "error", message = "數(shù)據(jù)驗(yàn)證失敗" }); 
  15.             } 
  16.             string ip = GetRemoteIpAddress(); 
  17.             var r = await UserApp.SaasLoginAsync(model.Account, model.Password, ip); 
  18.             if (!string.IsNullOrEmpty(r.Error)) 
  19.             { 
  20.                 return Json(new { state = "error", message = r.Error }); 
  21.             } 
  22.             var claims = new List<Claim> 
  23.                                         { 
  24.                                             new Claim(ClaimTypes.UserData, getCurrentUser(r.User, ip).ToString()), 
  25.                                         }; 
  26.             var claimsIdentity = new ClaimsIdentity( 
  27.                 claims, CookieAuthenticationDefaults.AuthenticationScheme); 
  28.             var authProperties = new AuthenticationProperties 
  29.             { 
  30.                 ExpiresUtc = DateTimeOffset.Now.AddMinutes(120) 
  31.             }; 
  32.             await HttpContext.SignInAsync( 
  33.                 CookieAuthenticationDefaults.AuthenticationScheme, 
  34.                 new ClaimsPrincipal(claimsIdentity), 
  35.                 authProperties); 
  36.             return Json(new { state = "success", message = "登錄成功。", returnUrl = RedirectToLocal(returnUrl) }); 
  37.         } 

SignInAsync 創(chuàng)建加密的 cookie,并將其添加到當(dāng)前響應(yīng)中。如果未指定 AuthenticationScheme,則使用默認(rèn)方案。

ASP.NET Core 的數(shù)據(jù)保護(hù)系統(tǒng)用于加密。對于托管在多臺計(jì)算機(jī)上的應(yīng)用程序、跨應(yīng)用程序或使用 web 場進(jìn)行負(fù)載平衡,請將數(shù)據(jù)保護(hù)配置為使用相同的密鑰環(huán)和應(yīng)用程序標(biāo)識符。

注銷

若要注銷當(dāng)前用戶并刪除其 cookie,請調(diào)用 SignOutAsync:

  1. /// <summary> 
  2.         /// 
  3.         /// </summary> 
  4.         /// <returns></returns
  5.         [HttpPost] 
  6.         [ValidateAntiForgeryToken] 
  7.         public async Task<IActionResult> LogOff() 
  8.         { 
  9.             if (bool.Parse(Configuration.GetSection("IsIdentity").Value)) 
  10.             { 
  11.                 return SignOut("Cookies""oidc"); 
  12.             } 
  13.             else 
  14.             { 
  15.                 if (User.Identity.IsAuthenticated) 
  16.                 { 
  17.                     string userdata = User.Claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value; 
  18.                     await UserApp.LogOffAsync(CurrentUser.FromJson(userdata)); 
  19.                 } 
  20.                 await HttpContext.SignOutAsync( 
  21.                  CookieAuthenticationDefaults.AuthenticationScheme); 
  22.                 return RedirectToAction(actionName: nameof(Login), controllerName: "Account"); 
  23.             } 
  24.         } 

參考資料

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

 

責(zé)任編輯:武曉燕 來源: UP技術(shù)控
相關(guān)推薦

2009-07-29 12:55:44

ASP.NET身份驗(yàn)證

2011-05-23 10:37:03

2009-07-29 16:47:40

ASP.NET表單身份

2012-06-04 09:36:50

2024-05-06 00:00:00

ASP.NET授權(quán)機(jī)制

2021-01-31 22:56:50

FromServiceASP

2021-02-03 13:35:25

ASPweb程序

2021-03-03 22:37:16

MediatR中介者模式

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache緩存框架

2021-01-28 22:39:35

LoggerMessa開源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-02-07 17:29:04

監(jiān)視文件接口

2024-05-17 08:59:02

.NET對象映射庫

2014-12-11 10:05:13

ASP.NET

2021-01-26 14:57:00

中間件應(yīng)用模塊化

2021-04-12 07:03:10

輕量級模塊化框架
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號