asp.net-core – .net核心自定义身份验证中的User.Identity.IsAu
发布时间:2020-12-15 20:56:01 所属栏目:asp.Net 来源:网络整理
导读:任何人都可以检查下面的代码,让我知道为什么我总是假的(User.Identity.IsAuthenticated)??我正确地在浏览器上获取cookie 能够从Claim获得价值,但“User.Identity.IsAuthenticated”总是错误的. public async TaskIActionResult Login(string phoneNumber,int
任何人都可以检查下面的代码,让我知道为什么我总是假的(User.Identity.IsAuthenticated)??我正确地在浏览器上获取cookie
能够从Claim获得价值,但“User.Identity.IsAuthenticated”总是错误的. public async Task<IActionResult> Login(string phoneNumber,int otp,string returnUrl) { if (this.accountService.ValidateOTP(phoneNumber,otp)) { var claims = new List<Claim> { new Claim(ClaimTypes.MobilePhone,phoneNumber),new Claim(ClaimTypes.Name,phoneNumber) }; var userIdentity = new ClaimsIdentity(); userIdentity.AddClaims(claim); ClaimsPrincipal userPrincipal = new ClaimsPrincipal(userIdentity); await HttpContext.Authentication.SignOutAsync("MyCookieMiddlewareInstance"); await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance",userPrincipal,new AuthenticationProperties { ExpiresUtc = DateTime.UtcNow.AddMinutes(20),IsPersistent = false,AllowRefresh = false }); if (string.IsNullOrWhiteSpace(returnUrl)) { return RedirectToAction("Create","Ad"); } else { return Redirect(returnUrl); } } return BadRequest(); } 解决方法
当ClaimsIdentity.AuthenticationType为null或为空时,ClaimsIdentity.IsAuthenticated返回false.为避免这种情况,请停止使用无参数的ClaimsIdentity构造函数并使用接受authenticationType参数的重载:
var userIdentity = new ClaimsIdentity("Custom"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – __doPostBack重新加载整个页面而不仅仅是Update
- asp.net-core – 简化的IOptions方法
- .Net Core技术研究-WebApi迁移ASP.NET Core2.0
- asp.net-mvc – 通用列表属性的必需属性
- asp.net – 无法转换类型为’System.Web.UI.LiteralControl
- 如何在ASP.NET MVC 4中选择自动完成文本字段值时进行另一个
- 为什么ASP.NET框架在响应中添加“X-Powered-By:ASP.NET”H
- asp.net-mvc – 使用Razor,如何使所有“bool?”属性显示为
- asp.net-mvc – 从Session_Start调用异步方法
- asp.net – 使用Elmah捕获背景线程中的异常
推荐文章
站长推荐
热点阅读