从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(); }更多推荐
发布评论