I have used the ClaimsAuthentionManager class to provide a mechanisme to transform incoming users with claims (roles). Here is some sample code of a custom ClaimsAuthenticationManager:
public class ClaimsTransformationModule : ClaimsAuthenticationManager
{
public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
{
if (incomingPrincipal != null && incomingPrincipal.Identity.IsAuthenticated == true)
{
Claim nameIdentifier = incomingPrincipal.Claims.Where(foo => foo.Type == ClaimTypes.Name).FirstOrDefault();
var roles = GetRoles(nameIdentifier.Value); // Get the roles from the backend based on the user
foreach (var role in roles) //This is the part applying roles to the Claim (user)
{
((ClaimsIdentity)incomingPrincipal.Identity).AddClaim(new Claim(ClaimTypes.Role, role));
}
((ClaimsIdentity)incomingPrincipal.Identity).AddClaim(new Claim(ClaimTypes.Sid, GetUserId(nameIdentifier.Value)));
}
return incomingPrincipal;
}
Then in the Web.config you can configure the system to use your custom Claims Manager:
<system.identityModel>
<identityConfiguration>
<claimsAuthenticationManager type="ClaimsTransformation.ClaimsTransformationModule, ClaimsTransformation" />
Then to get the roles for the currently logged on user you go:
var user = ClaimsPrincipal.Current;
bool isInRole = user.IsInRole(roleName);
But have a look at the leastprivilege.com site for more information.
Cheers Frank