Created
May 29, 2014 13:39
-
-
Save alexsandro-xpt/cbf2de56d95ad638df01 to your computer and use it in GitHub Desktop.
Add Role claim functionality https://github.com/aspnet/Identity/issues/62
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override async Task<ClaimsIdentity> CreateAsync(UserManager<IUser, string> userManager, IUser user, string authenticationType) | |
{ | |
var id = new ClaimsIdentity(authenticationType, UserNameClaimType, null); | |
id.AddClaim(new Claim(UserIdClaimType, user.Id.ToString(), ClaimValueTypes.Integer)); | |
id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String)); | |
id.AddClaim(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String)); | |
if (userManager.SupportsUserRole) | |
{ | |
var rolesGenericas = await userManager.GetRolesGenerica(user.Id); | |
var roles = await userManager.GetRolesNonGenericFromContext(user.Id, _contextoId); | |
var allRoles = rolesGenericas.Concat(roles).ToList(); | |
foreach (var role in allRoles) | |
{ | |
if (role.Fluxos != null) | |
{ | |
foreach (var fluxo in role.Fluxos) | |
{ | |
id.AddClaim(new Claim("BusinessFlow", fluxo.Controle, ClaimValueTypes.String)); | |
} | |
} | |
if (role.Administracoes != null) | |
{ | |
foreach (var administracao in role.Administracoes) | |
{ | |
id.AddClaim(new Claim("AdministrationFlow", administracao.Controle, | |
ClaimValueTypes.String)); | |
} | |
} | |
id.AddClaim(new Claim(RoleClaimType, role.Nome, ClaimValueTypes.String)); | |
} | |
} | |
id.AddClaim(new Claim("UserContextName", _contextoNome, ClaimValueTypes.String)); | |
id.AddClaim(new Claim("UserContextId", _contextoId.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer)); | |
if (userManager.SupportsUserClaim) | |
{ | |
id.AddClaims(await userManager.GetClaimsAsync(user.Id)); | |
} | |
return await Task.FromResult(id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment