0
votes

I'm using asp.net identity with ClaimsPrincipal and ClaimsIdentity. The identity is created by a custom usermanager and sent to the clients as a bearer token.

All works fine, but I would like to instruct asp.net to use a custom claim type for role, instead of the standard System.Security.Claims.ClaimTypes.Role (http://schemas.microsoft.com/ws/2008/06/identity/claims/role).

Is it possibile?

Edit: I would like to use the standard Authorize attribute with role constructor, ex: Authorize(Roles="staff") and let the framework to check the claims, but my custom role claim type ("myapproleclaim") instead of the standard one.

1
Yes possible. But what exactly you wanna do? Is what you wanna do is somthing like this ? User.IsInRole('superadmin') ? dothis() : dootherstuff() - mehmet mecek

1 Answers

1
votes

You can do things like,

public class CustomPrinciple : ClaimsPrincipal
{
    public CustomPrinciple(ClaimsIdentity identity) : base(identity)
    {
    }

    public override bool IsInRole(string role)
    {
        return HasClaim("myRoleClaimType", role);
    }
}

[TestClass]
public class CustomRoleTest
{
    [TestMethod]
    public void testing_custom_role_type()
    {
        var identity = new ClaimsIdentity();
        identity.AddClaim(new Claim("myRoleClaimType", "role1"));
        var principle = new CustomPrinciple(identity);

        Assert.IsTrue(principle.IsInRole("role1"));
        Assert.IsFalse(principle.IsInRole("role2"));
    }
}