Skip to content

Instantly share code, notes, and snippets.

@bjcull
Last active May 18, 2018 14:05
Show Gist options
  • Save bjcull/a4c198305fe5d4286144ad5bdc3b5bc8 to your computer and use it in GitHub Desktop.
Save bjcull/a4c198305fe5d4286144ad5bdc3b5bc8 to your computer and use it in GitHub Desktop.
Adds all custom claims to the asp.net core identity
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);
}
}
// 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