Created
December 1, 2014 19:37
-
-
Save brspurri/9dfeed196f5bf539189c to your computer and use it in GitHub Desktop.
Code to go with my comment at: http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/
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 void ConfigureOAuth(IAppBuilder app) | |
{ | |
var issuer = "<the_same_issuer_as_AuthenticationServer.Api>"; | |
// Api controllers with an [Authorize] attribute will be validated with JWT | |
var audiences = DatabaseAccessLayer.GetAllowedAudiences(); // Gets a list of audience Ids, secrets, and names (although names are unused) | |
// List the | |
List<string> audienceId = new List<string>(); | |
List<IIssuerSecurityTokenProvider> providers = new List<IIssuerSecurityTokenProvider>(); | |
foreach (var aud in audiences) { | |
audienceId.Add(aud.ClientId); | |
providers.Add(new SymmetricKeyIssuerSecurityTokenProvider(issuer, TextEncodings.Base64Url.Decode(aud.ClientSecret))); | |
} | |
app.UseJwtBearerAuthentication( | |
new JwtBearerAuthenticationOptions | |
{ | |
AuthenticationMode = AuthenticationMode.Active, | |
AllowedAudiences = audienceId.ToArray(), | |
IssuerSecurityTokenProviders = providers.ToArray(), | |
Provider = new OAuthBearerAuthenticationProvider | |
{ | |
OnValidateIdentity = context => | |
{ | |
context.Ticket.Identity.AddClaim(new System.Security.Claims.Claim("newCustomClaim", "newValue")); | |
return Task.FromResult<object>(null); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment