ASP.NET Identity “Role-based” Claims

后端 未结 3 1678
小蘑菇
小蘑菇 2020-12-30 00:17

I understand that I can use claims to make statements about a user:

var claims = new List();
claims.Add(new Claim(ClaimTypes.Name, \"Peter\"));
         


        
3条回答
  •  Happy的楠姐
    2020-12-30 00:52

    You need to specify the Role in a claim with a type of ClaimsType.Role and then specify the claim type that contains the role in the ClaimsIdentity as shown below.

    var claimsIdentity = new ClaimsIdentity(new[]
    {
        new Claim(ClaimTypes.Email, "peter@domain.com"),
        new Claim(ClaimTypes.Name, "Peter"),
        new Claim(ClaimTypes.Role, "SuperAdmin"),
    },
    "ApplicationCookie", ClaimTypes.Email, ClaimTypes.Role);
    

    This will then allow you to use the [Authorize(Roles = "SuperAdmin")] attribute in your controllers.

提交回复
热议问题