asp.net-core – .net核心自定义身份验证中的User.Identity.IsAuthenticated始
发布时间:2020-09-01 19:03:12 所属栏目:asp.Net 来源:互联网
导读:任何人都可以检查下面的代码,让我知道为什么我总是假的(User.Identity.IsAuthenticated)??我正确地在浏览器上获取cookie 能够从Claim获得价值,但“User.Identity.IsAuthenticated”总是错误的. public async TaskIActionResult Login(string phoneNumber, int
任何人都可以检查下面的代码,让我知道为什么我总是假的(User.Identity.IsAuthenticated)??我正确地在浏览器上获取cookie
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 – 启用Application Insights会使Web应用
- asp.net-mvc – 为什么DropDownListFor会在提交后
- .net – CS0012:类型’System.Data.Linq.DataCo
- 在ASP.Net MVC应用程序中放置初始化代码的位置?
- asp.net – jquery getJson没有将任何值传递给控
- asp.net-mvc – ASP.NET MVC如何实现返回上一页的
- asp.net-mvc – 为什么我的视图模型中的内部成员
- ASP.NET通过自定义函数实现对字符串的大小写切换
- 在IIS上部署ASP.NET Core项目的图文方法
- asp.net – IIS Web Garden中的Singleton对象
热点阅读