内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Asp.netcore中怎么使用cookie驗證身份

今天就跟大家聊聊有關Asp.net core中怎么使用cookie驗證身份,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

今天就跟大家聊聊有關Asp.net core中怎么使用cookie驗證身份,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

為中方等地區(qū)用戶提供了全套網頁設計制作服務,及中方網站建設行業(yè)解決方案。主營業(yè)務為做網站、成都做網站、中方網站設計,以傳統(tǒng)方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

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

配置

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

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

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

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

默認情況下,身份驗證 cookie 的 IsEssential 屬性設置為 true。當站點訪問者未同意數據收集時,允許使用身份驗證  cookie。

登錄

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

使用任何所需的 Claim創(chuàng)建 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 創(chuàng)建加密的 cookie,并將其添加到當前響應中。如果未指定 AuthenticationScheme,則使用默認方案。

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

注銷

若要注銷當前用戶并刪除其 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");             }         }

新聞名稱:Asp.netcore中怎么使用cookie驗證身份
URL標題:http://www.rwnh.cn/article34/csjhpe.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站制作、小程序開發(fā)、網站維護、虛擬主機、動態(tài)網站、標簽優(yōu)化

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

搜索引擎優(yōu)化
高邮市| 东方市| 三亚市| 乌兰浩特市| 江山市| 塘沽区| 通渭县| 潮州市| 萨迦县| 武夷山市| 古田县| 庆阳市| 罗田县| 安福县| 镇平县| 巩义市| 屯留县| 松江区| 永登县| 南雄市| 绩溪县| 阿尔山市| 长汀县| 柳州市| 浦江县| 乐亭县| 保德县| 巢湖市| 锡林郭勒盟| 汉阴县| 常德市| 武宣县| 资兴市| 景洪市| 黄大仙区| 读书| 包头市| 洪江市| 谢通门县| 九台市| 区。|