? 亚洲综合在线另类色区小说_asp.net core中如何使用cookie身份驗證-天津九安特機電工程有限公司

亚洲女同成aV人片在线观看|亚洲www啪成人一区二区麻豆|亚洲国产中日韩精品综合|亚洲国产成人精品一级片|亚洲无码在线视频免费

?

asp.net core中如何使用cookie身份驗證

這篇文章主要介??紹了asp.ne(′_`)t core中如何使用cookie身份驗證的中何相關(guān)資料,文中通過(guò)示(shi)例代碼介紹的使用身份非常詳細,對大家的驗證學(xué)習或者工作具有一定的參考學(xué)習價(jià)值,需要的中何朋友們下面隨著(zhù)小編來(lái)一起學(xué)習學(xué)習吧

背景

ASP.NET Core Identity 是一個(gè)完整的全功能身份驗證提供程序,用于創(chuàng )建和維護登錄名。使用身份 但是驗證, cookie 不能使用(′_ゝ`)基于的中何身份驗證提供程序 ASP.NET Core?? Identity 。

配置

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

services??.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();

app.??UseA??uthentication();

AuthenticationSch??eme 傳遞到 AddAuthentication 設置應用程序的默認??身份驗證方案。如果有多個(gè) cookie 身份驗證實(shí)例,中何并且你想要使用特定(ding)方案進(jìn)行授權,使用身份AuthenticationScheme 會(huì )很有用。驗證將 AuthenticationScheme(′▽?zhuān)?) 設置為??Cook??ieAuthenticationDefaults。中何AuthenticationScheme為方案提供值 "cookie"。使用身份可以提供任何用于區分方案的驗證字符串值。

應用的身份驗證方案不同于應用的 cookie 身份驗證方案。如果未向 AddCookie提供 cookie 身份??驗證方案,則使用? CookieAut??henticationDefaults.AuthenticationScheme ("Cookie")。

默認情況下,??身份驗證 cookie 的 IsEssential 屬性設置為(′▽?zhuān)? true。當站點(diǎn)訪(fǎng)問(wèn)者未同意數據收集時(shí),允許使用身份驗證 cook( ?ヮ?)ie。

登錄

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

使用任何所需的 Claim創(chuàng )建 ClaimsIdentity,并調用 Si??gヾ(′?`)?nInAsync 以登錄用戶(hù):

//??/ <summary>
///
/// </summary>
/// <param name='model'></param>
/// <param name='returnUrl'></param>
/// <retur??ns></returns>
[HttpPost???]
[AllowAttribute]
[ValidateAntiForgeryToken]
public async Task<IActionR??esult> Login(LoginM(???)odel model, string returnUrl = null)
{
if (!ModelS??tate.IsValid)
{
return Json(┐(′?`)┌new { state = "error", message='數據驗證失敗' });
}
string ip = GetRemoteIpAddress();
var r = await UserApp.SaasLoginAsync(model.Account, model.Password, ip);
if (!string.Is(°□°)NullOrEmpty(r.Error))
{
return Json(new { state='error', messag(╯‵□′)╯e = r.Error })(?_?;);
}
var claims = new List<Claim>
{
new Claim(Claim(′_ゝ`)Types.UserDa??ta, getCurrentUser(r.User, ip).ToStrin??g()),
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new?? AuthenticationProperties
{
ExpiresUtc = Daˉ\_(ツ)_/ˉteTimeOffset.Now.AddMinutes(1??20)
};
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claim(′?_?`)sIdentit(′?ω?`)y),
authProperties);
return Json(ne??w { state='success', message='登錄成功。', returnUrl = RedirectToLocal(returnUrl) });
}

SignInAsync 創(chuàng )建加密的 co??okie,并將其添加到當前響應中。如果未指定 Authen(′?`)ticationScheme,則使用默認方案。

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

注(╯‵□′)╯銷(xiāo)

若要注銷(xiāo)當前用戶(hù)并刪除其 cookie,請調用 SignOutAsync:

/// <summary>
///
/// </summary>
/// <returns>??</returns>
[HttpPost]
[ValidateAntiForgeryT(′_ゝ`)oken]
public async Tas??k<IActionResult> LogOff()
{
if (bool.Parse(Configuratio??n.GetSection("IsIdentity").Value))
{
return SignOut("Cookie??s", "oidc");
}
else
{
if (User.Identity.IsAuthenticated)
{
string userdata = User.Clai??ms.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value;(?????)
await UserApp.LogOffAsync(CurrentU(′?`)ser.FromJson(userdata));
}
await HttpContext.SignOutAsync(
CookieAuthヽ(′▽?zhuān)?ノenticationDefヽ(′ー`)ノaults.AuthenticationScheme);
return RedirectToAction(actionName: nameof(Login), controllerName: "Account");
}
}

參考資料

https://d(⊙_⊙)ocs.microsoft.com/zh-cn/aspnet/core/security/authentication/?v??iew=aspnetcore-5.0

到此這篇關(guān)于asp.net core中如何使用cookie身份驗證的文章就介紹到這了,更多相關(guān)asp.net core用cookie身份驗證內容請搜??索腳本之家以前的(de)文章或繼續瀏覽下面的相關(guān)文章希望大家以后多??多支持腳本之家!

來(lái)源:腳本之家

鏈接:https://www.jb??51.net/article/202963.htm

  1. 上一篇:網(wǎng)絡(luò )傳媒是干什么的_赫章媒體網(wǎng)絡(luò )推廣是什么
  2. 下一篇:沒(méi)有了;

其他產(chǎn)品

亚洲女同成aV人片在线观看|亚洲www啪成人一区二区麻豆|亚洲国产中日韩精品综合|亚洲国产成人精品一级片|亚洲无码在线视频免费 巍山| 荆门市| 东光县| 巍山| 四会市| 锦州市| 昭通市| 郯城县| 齐齐哈尔市| 扶沟县| 香河县| 临澧县| 旅游| 含山县| 青海省| 慈利县| 临夏县| 鄂托克旗| 会理县| 顺平县| 青海省| 垣曲县| 台州市| 新宁县| 德格县| 台东县| 天镇县| 阿巴嘎旗| 辽宁省| 二连浩特市| 德化县| 抚顺县| 房产| 和平区| 襄汾县| 巨鹿县| 岗巴县| 安陆市| 讷河市| 游戏| 舞钢市| http://444 http://444 http://444 http://444 http://444 http://444