Last active
May 18, 2018 14:05
-
-
Save bjcull/a4c198305fe5d4286144ad5bdc3b5bc8 to your computer and use it in GitHub Desktop.
Adds all custom claims to the asp.net core identity
This file contains hidden or 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 class AddAllClaimAction : ClaimAction | |
{ | |
public AddAllClaimAction() : base(null, null) | |
{ | |
} | |
public override void Run(JObject userData, ClaimsIdentity identity, string issuer) | |
{ | |
var claims = userData.Properties() | |
.Where(x => !x.Value.HasValues) | |
.Select(x => new Claim(x.Name, x.Value.Value<string>(), null, issuer)) | |
.ToList(); | |
var nonDuplicateClaims = claims.Where(x => !identity.HasClaim(x.Type, x.Value)); | |
identity.AddClaims(nonDuplicateClaims); | |
} | |
} |
This file contains hidden or 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
// Usage (inside startup.cs) | |
services.AddAuthentication() | |
.AddOpenIdConnect("oidc", options => | |
{ | |
// ... | |
options.ClaimActions.Add(new AddAllClaimAction()); | |
// ... | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment