国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET Cookie是怎么生成的(推薦)

2024-07-10 12:49:42
字體:
供稿:網(wǎng)友

可能有人知道Cookie的生成由machineKey有關(guān),machineKey用于決定Cookie生成的算法和密鑰,并如果使用多臺服務(wù)器做負(fù)載均衡時,必須指定一致的machineKey用于解密,那么這個過程到底是怎樣的呢?

如果需要在.NET Core中使用ASP.NET Cookie,本文將提到的內(nèi)容也將是一些必經(jīng)之路。

抽絲剝繭,一步一步分析
首先用戶通過AccountController->Login進(jìn)行登錄:

//// POST: /Account/Loginpublic async Task<ActionResult> Login(LoginViewModel model, string returnUrl){ if (!ModelState.IsValid) { return View(model); } var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success:  return RedirectToLocal(returnUrl); // ......省略其它代碼 }}

它調(diào)用了SignInManager的PasswordSignInAsync方法,該方法代碼如下(有刪減):

public virtual async Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout){ // ...省略其它代碼 if (await UserManager.CheckPasswordAsync(user, password).WithCurrentCulture()) { if (!await IsTwoFactorEnabled(user)) {  await UserManager.ResetAccessFailedCountAsync(user.Id).WithCurrentCulture(); } return await SignInOrTwoFactor(user, isPersistent).WithCurrentCulture(); } // ...省略其它代碼 return SignInStatus.Failure;}

想瀏覽原始代碼,可參見官方的Github鏈接:

https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.Owin/SignInManager.cs#L235-L276

可見它先需要驗證密碼,密碼驗證正確后,它調(diào)用了SignInOrTwoFactor方法,該方法代碼如下:

private async Task<SignInStatus> SignInOrTwoFactor(TUser user, bool isPersistent){ var id = Convert.ToString(user.Id); if (await IsTwoFactorEnabled(user) && !await AuthenticationManager.TwoFactorBrowserRememberedAsync(id).WithCurrentCulture()) { var identity = new ClaimsIdentity(DefaultAuthenticationTypes.TwoFactorCookie); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, id)); AuthenticationManager.SignIn(identity); return SignInStatus.RequiresVerification; } await SignInAsync(user, isPersistent, false).WithCurrentCulture(); return SignInStatus.Success;}

該代碼只是判斷了是否需要做雙重驗證,在需要雙重驗證的情況下,它調(diào)用了AuthenticationManager的SignIn方法;否則調(diào)用SignInAsync方法。SignInAsync的源代碼如下:

public virtual async Task SignInAsync(TUser user, bool isPersistent, bool rememberBrowser){ var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture(); // Clear any partial cookies from external or two factor partial sign ins AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); if (rememberBrowser) { var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id)); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity); } else { AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity); }}            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 万载县| 周至县| 玛沁县| 称多县| 望江县| 镶黄旗| 玉门市| 新源县| 曲沃县| 抚远县| 新密市| 临颍县| 武城县| 安陆市| 西乌| 元阳县| 辽宁省| 南华县| 博客| 梨树县| 三台县| 博野县| 扬州市| 霍城县| 图木舒克市| 黄陵县| 藁城市| 白城市| 敖汉旗| 伊春市| 河西区| 平谷区| 梧州市| 开江县| 鄂伦春自治旗| 大田县| 沈丘县| 鹿邑县| 龙口市| 筠连县| 呈贡县|