asp.net-core – .net核心自定义身份验证中的User.Identity.IsAuthenticated始
发布时间:2020-05-22 12:31:54 所属栏目: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生成器在睡眠期间遇到ThreadAb
- asp.net-mvc-4 – signalR:/ signalr/hubs不生成
- asp.net-membership – MiniProfiler和SqlMembershipProvid
- asp.net-mvc – 如何只允许mvc4 razor中的文本框中的数字
- 如何在ASP.net runat =’server’标记中使用传统的HTML id属
- 如何将应用程序从经典ASP迁移到ASP.NET MVC?
- .net – RegularExpressionValidator使用除Regex之外的其他
- ASP.NET应用程序配置IIS后,会出现错误
- asp.net – CSS压缩和组合/ js缩小 – 在运行时或构建时更好
- ASP.NET UpdatePanel库引用错误
推荐文章
站长推荐
- ASP.NET Razor查看Html.TextBox大小/宽度
- asp.net-web-api – 如何从ASP.net 5 web api返回
- asp.net-mvc – ASP.NET MVC验证的唯一性
- asp.net-mvc-3 – 无法加载文件或程序集“System
- asp.net-mvc – 发布ASP.NET MVC 3 Html.TextAre
- asp.net-mvc – ASP.NET MVC缓存如何为AJAX请求工
- 为什么在ASP.NET MVC中暂停超时?
- ASP.NET MVC4 Razor模板简易分页效果
- ASP.NET Core 1.0 F#项目
- asp.net-mvc – 使用ViewModel的MVC 3动态表单
热点阅读
