How to create a list of Roles in Claim using ASP.NET Core

Asked

Viewed 327 times

0

I made an authentication using Claims with cookies in the 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 because he only accepts one element of the kind string and my list has more than one element (like string).

No one was accepted foreach within.

 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");

    }

1 answer

0


You need to add individually, as I use the Authorize of the Asp.net core itself I do so, it is responsible to serialize it internally:

claims.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role)));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.