1

I have this method to sign in a user using .Net Core Cookie Authentication.

    [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();
    }

I am filling up ClaimTypes.Name with the user name being posted up to the login View Model. Is there like a ClaimTypes.Roles value to fill up? I need to be able to use "User.IsInRole(...)".

This would be a collection of roles of course for a user. Not just one role. Does anyone know how to do this?

Sam
  • 4,766
  • 11
  • 50
  • 76

1 Answers1

1

Add like this:

claims.Add(new Claim(ClaimTypes.Role, p.Role.RoleCd));
Sam
  • 4,766
  • 11
  • 50
  • 76