Last active
November 23, 2019 15:23
-
-
Save mattbajorek/a92b2a83a5799f1c137efa16f55dedf8 to your computer and use it in GitHub Desktop.
Authentication
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 ConfigureServices(IServiceCollection services) | |
{ | |
var Region = Configuration["AWSCognito:Region"]; | |
var PoolId = Configuration["AWSCognito:PoolId"]; | |
var AppClientId = Configuration["AWSCognito:AppClientId"]; | |
services | |
.AddAuthentication(options => | |
{ | |
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}) | |
.AddJwtBearer(options => | |
{ | |
options.SaveToken = true; | |
options.TokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKeyResolver = (s, securityToken, identifier, parameters) => | |
{ | |
// Get JsonWebKeySet from AWS | |
var json = new WebClient().DownloadString(parameters.ValidIssuer + "/.well-known/jwks.json"); | |
// Serialize the result | |
return JsonConvert.DeserializeObject<JsonWebKeySet>(json).Keys; | |
}, | |
ValidateIssuer = true, | |
ValidIssuer = $"https://cognito-idp.{Region}.amazonaws.com/{PoolId}", | |
ValidateLifetime = true, | |
LifetimeValidator = (before, expires, token, param) => expires > DateTime.UtcNow, | |
ValidateAudience = true, | |
ValidAudience = AppClientId, | |
}; | |
}); | |
} | |
public static void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
... | |
app.UseAuthentication(); | |
... | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment