User.Identity始终为null
发布时间:2023-02-17 09:05:25 所属栏目:asp.Net 来源:互联网
导读:如何解决User.Identity始终为null? 我需要检查用户是否已登录。在View中,我正在像这样检查它; @if (User.Identity.IsAuthenticated) { //links... } else { //links... } 尽管我在登录功能中登录了SignInAsync,但始终返回false,并且身份为空。我试图更
{ endpoints.MapControllerRoute( name: "default",pattern: "{controller=Home}/{action=Index}"); }); } 登录: [HttpPost("/login")] public async Task<IActionResult> Login(LoginModel model) { if (!ModelState.IsValid) return View(model); var myContent = JsonConvert.SerializeObject(model); var stringContent = new StringContent(myContent,System.Text.Encoding.UTF8,MediaTypeNames.Application.Json); using (var postTask = await ApiHelper.apiclient.PostAsync("Auth/Login",stringContent)) { string jwt = await postTask.Content.ReadAsstringAsync(); var handler = new JwtSecurityTokenHandler(); var token = handler.ReadJwtToken(JwtExtension.CorrectJwtFormat(jwt)); var claims = token.Payload.Claims.ToList(); var claimsIdentity = new ClaimsIdentity( claims,CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties() { AllowRefresh = true,ExpiresUtc = DateTimeOffset.Now.AddMonths(1),IsPersistent = true }; await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(claimsIdentity),authProperties); return RedirectToAction("index","home"); } } API运作良好,给我带来了7条声明,而且ClaimsIdentity也包含它们。并重定向到索引。我做错了什么我只是不知道。 补充:另外我不能使用Authorize属性。因此,任何地方都没有授权。 (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net – 为什么HttpContext.Current.User.Ide
- 在IIS上部署ASP.NET Core项目的图文方法
- ASP.NET web.config中数据库连接字符串connectio
- asp.net – 确定当前页面是否需要授权?
- Asp.net webForm设置允许表单提交Html的方法
- asp.net-core – .NET Core SDK安装程序无法在Wi
- asp.net-mvc – Nhibernate / MVC:在View中处理
- asp.net-core – 我为什么要选择带有.Net核心的A
- asp.net-mvc – 如何将MVC 5 IdentityModels.cs移
- asp.net – 为不同项目中的所有Web应用程序网页添
热点阅读