Authentication 为了使角色授权在.Net Core 2.0中发挥作用,我应该在ClaimsPrincipal中填写什么声明?
我使用此方法使用.Net Core Cookie身份验证登录用户Authentication 为了使角色授权在.Net Core 2.0中发挥作用,我应该在ClaimsPrincipal中填写什么声明?,authentication,authorization,asp.net-core-2.0,cookie-authentication,Authentication,Authorization,Asp.net Core 2.0,Cookie Authentication,我使用此方法使用.Net Core Cookie身份验证登录用户 [HttpPost] [AllowAnonymous] public async Task<IActionResult> Login(LoginVM loginModel) { var claims = new List<Claim> { new Claim(ClaimTypes.Name, loginModel.Use
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginVM loginModel)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, loginModel.UserName)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
// TODO: Validate against DB Staff/User.
// If valid, sign in.
await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity));
return Redirect(loginModel?.ReturnUrl ?? "/");
// Else return View();
}
[HttpPost]
[异名]
公共异步任务登录(LoginVM loginModel)
{
var索赔=新列表
{
新声明(ClaimTypes.Name、loginModel.UserName)
};
var claimsIdentity=新的claimsIdentity(索赔、CookieAuthenticationDefaults.AuthenticationScheme);
//TODO:根据数据库工作人员/用户进行验证。
//如果有效,请登录。
等待HttpContext.SignInAsync(newclaimsprincipal(claimsIdentity));
返回重定向(loginModel?.ReturnUrl??/);
//Else返回视图();
}
我正在填写ClaimTypes.Name,其中用户名将发布到登录视图模型。
是否有像ClaimTypes.Roles这样的值需要填充?
我需要能够使用“User.IsInRole(…)”
这当然是一个用户角色的集合。不仅仅是一个角色。
有人知道怎么做吗?添加如下内容:
claims.Add(new Claim(ClaimTypes.Role, p.Role.RoleCd));
您只需将多个
索赔类型。角色
索赔
添加到列表中即可。