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身份验证的文章就介绍到这了,更多相关asp.net core用cookie身份验证内容请搜索站长技术以前的文章或继续浏览下面的相关文章希望大家以后多多支持站长技术!
大家应该都知道,在静态页面提交表单到数据库很简单就是单纯的 form action="tes...
1.判断是否有注入 ;and1=1 ;and1=2 2.初步判断是否是mssql ;anduser0 3.注入参数...
jsp 开发之struts2中s:select标签的使用 1.第一个例子: s:select list="{'aa','...
LABVIEW如何连接SQL Server详细说明 一、整体流程图 二、Sql Sever 2012下载安装...
导读: 正则表达式是处理字符串类型的"核武器",不仅速度快,而且功能强大。本文...
本文转载自微信公众号「Java极客技术」,作者鸭血粉丝 。转载本文请联系Java极客...
最近出的每日一题里面,有一道是关于合并同类型数据为一行的题,使用SQL Server ...
分享背景 由于一直在虚拟机的状态下开发PHP,尝试一下mac本地搭建环境.mac本身是...
前言 上次咱们简单的学习了一下select的用法,一篇文章教会你进行Mysql数据库和...
支持多行同时拖拽,重复数据不重得添加,添加了图表右键菜单. 复制代码 代码如下...