How to create a list of roles in Claim using ASP.NET Core
I made an authentication using Claims
with cookies
in ASP.NET Core
.
In the method below, the object by parameter brings the login information and inside it has a list called PerfisDeAcesso
. How do I assign this list to ClaimTypes.Role
? I ask this because it only accepts a single element of type string
and my list has more than one element (of type string
).
Was not accepted a foreach
in.
private async Task<IActionResult> SignInAsync(Usuario usuario)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, usuario.CodUsuario),
new Claim(ClaimTypes.Name, usuario.Login),
new Claim(ClaimTypes.Role, /* lista com os perfis de acesso */)
};
var identity = new ClaimsIdentity(claims, "login");
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(principal);
return RedirectToAction("Index", "Home");
}
0
1 answers
You need to add individually, as I use the Authorize of itself asp.net core i do like this, it gets responsible in serializing this internally:
claims.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role)));
0
Author: Rafael, 2019-04-18 15:22:43