asp.net-core – .net核心自定义身份验证中的User.Identity.IsAuthenticated始
发布时间:2020-09-01 17:42:35 所属栏目: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-mvc – 图像URL中的符号MVC导致一个潜在危险的Requ
- asp.net简单生成XML文件的方法
- VS 2013 RC中缺少ASP.NET Web窗体脚手架功能
- asp.net-mvc-3 – MVC3正确的方式来改变每个请求的文化
- asp.net – MS Chart for .NET预定义调色板颜色列表?
- asp.net-mvc – 如何使用带有IEnumerable的Html.CheckBox(l
- asp.net – 部分视图呈现按钮点击
- asp.net(C#)把汉字转化成全拼音函数(全拼)
- ASP.NET Webforms,用户控件中的JavaScript
- asp.net+ajaxfileupload.js 实现文件异步上传代码分享
推荐文章
站长推荐
- asp.net实现生成缩略图及给原始图加水印的方法示
- asp.net – 我们可以在视图状态中放置哪些类型的
- asp.net-mvc – 在MVC Controller中访问GET参数
- asp.net – 登录后对Membership.GetAllUsers()的
- asp.net-mvc-3 – “区域”文件夹中的样式,脚本和
- asp.net-core – .NET Core SDK安装程序无法在Wi
- asp.net-mvc – 在ASP.NET MVC中实现工作单元的方
- .net – RESTful WCF的裸最低配置
- asp.net-mvc – 有条件地在webgrid中显示图像 –
- Asp.net Core 1.1 升级后操作mysql出错的解决办法
热点阅读