从Core MVC的Cookie中的声明中检索用户标识(Retrieve Userid from a claims in a cookie in Core MVC)

编程入门 行业动态 更新时间:2024-10-17 13:34:16
从Core MVC的Cookie中的声明中检索用户标识(Retrieve Userid from a claims in a cookie in Core MVC)

我想在ASP.NET Core MVC中的cookie中存储userId。 我在哪里可以访问它?

登录:

var claims = new List<Claim> { new Claim(ClaimTypes.NameIdentifier, "testUserId") }; var userIdentity = new ClaimsIdentity(claims, "webuser"); var userPrincipal = new ClaimsPrincipal(userIdentity); HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties { AllowRefresh = false });

登出:

User.Identity.GetUserId(); // <-- 'GetUserId()' doesn't exists!? ClaimsPrincipal user = User; var userName = user.Identity.Name; // <-- Is null. HttpContext.Authentication.SignOutAsync("Cookie");

这在MVC 5中是可能的------------------->

登录:

// Create User Cookie var claims = new List<Claim>{ new Claim(ClaimTypes.NameIdentifier, webUser.Sid) }; var ctx = Request.GetOwinContext(); var authenticationManager = ctx.Authentication; authenticationManager.SignIn( new AuthenticationProperties { AllowRefresh = true // TODO }, new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie) );

获取UserId:

public ActionResult TestUserId() { IPrincipal iPrincipalUser = User; var userId = User.Identity.GetUserId(); // <-- Working }

更新 - 添加了声明的屏幕截图,它是null -------

userId也是null 。

I want to store a userId in a cookie, in ASP.NET Core MVC. Where can I access it?

Login:

var claims = new List<Claim> { new Claim(ClaimTypes.NameIdentifier, "testUserId") }; var userIdentity = new ClaimsIdentity(claims, "webuser"); var userPrincipal = new ClaimsPrincipal(userIdentity); HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties { AllowRefresh = false });

Logout:

User.Identity.GetUserId(); // <-- 'GetUserId()' doesn't exists!? ClaimsPrincipal user = User; var userName = user.Identity.Name; // <-- Is null. HttpContext.Authentication.SignOutAsync("Cookie");

It's possible in MVC 5 ------------------->

Login:

// Create User Cookie var claims = new List<Claim>{ new Claim(ClaimTypes.NameIdentifier, webUser.Sid) }; var ctx = Request.GetOwinContext(); var authenticationManager = ctx.Authentication; authenticationManager.SignIn( new AuthenticationProperties { AllowRefresh = true // TODO }, new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie) );

Get UserId:

public ActionResult TestUserId() { IPrincipal iPrincipalUser = User; var userId = User.Identity.GetUserId(); // <-- Working }

Update - Added screenshot of the Claims which are null -------

userId is also null.

最满意答案

你应该可以通过HttpContext获取它:

var userId = context.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;

在示例上下文中是HttpContext。

Startup.cs(就像模板网站中的基础一样):

public void ConfigureServices(IServiceCollection services) { services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseIdentity(); app.UseMvc(); }

You should be able to get it via the HttpContext:

var userId = context.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;

In the example context is the HttpContext.

The Startup.cs (just the basics as in the template website):

public void ConfigureServices(IServiceCollection services) { services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseIdentity(); app.UseMvc(); }

更多推荐

本文发布于:2023-08-06 23:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标识   声明   用户   Cookie   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!