ASP.NET Core – Add role claim to User

Well beside the answers, I just found the answer which is totally predefined in asp .net core. When you are adding claims just :

var claims = new List<Claim>
{
    new Claim(ClaimTypes.Name, UserName),
    new Claim(ClaimTypes.Role, "User"),
    new Claim(ClaimTypes.Role, "Admin"),
    new Claim(ClaimTypes.Role, Watever)
};

after that you can just use it as said:

[Authorize(Roles = "Watever")]

or

User.IsInRole("Watever")

Leave a Comment