Last active
December 14, 2016 16:47
-
-
Save samueleresca/4c8a8844a944005a4086fa20b0ebbfad to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Text; | |
using System.Security.Claims; | |
using System.Security.Principal; | |
using System.Threading.Tasks; | |
using CustomTokenAuthProvider; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.IdentityModel.Tokens; | |
using Microsoft.Extensions.Options; | |
namespace Blog.TokenAuthGettingStarted | |
{ | |
public partial class Startup | |
{ | |
private void ConfigureAuth(IApplicationBuilder app) | |
{ | |
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("TokenAuthentication:SecretKey").Value)); | |
var tokenProviderOptions = new TokenProviderOptions | |
{ | |
Path = Configuration.GetSection("TokenAuthentication:TokenPath").Value, | |
Audience = Configuration.GetSection("TokenAuthentication:Audience").Value, | |
Issuer = Configuration.GetSection("TokenAuthentication:Issuer").Value, | |
SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256), | |
IdentityResolver = GetIdentity | |
}; | |
var tokenValidationParameters = new TokenValidationParameters | |
{ | |
// The signing key must match! | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKey = signingKey, | |
// Validate the JWT Issuer (iss) claim | |
ValidateIssuer = true, | |
ValidIssuer = Configuration.GetSection("TokenAuthentication:Issuer").Value, | |
// Validate the JWT Audience (aud) claim | |
ValidateAudience = true, | |
ValidAudience = Configuration.GetSection("TokenAuthentication:Audience").Value, | |
// Validate the token expiry | |
ValidateLifetime = true, | |
// If you want to allow a certain amount of clock drift, set that here: | |
ClockSkew = TimeSpan.Zero | |
}; | |
app.UseJwtBearerAuthentication(new JwtBearerOptions | |
{ | |
AutomaticAuthenticate = true, | |
AutomaticChallenge = true, | |
TokenValidationParameters = tokenValidationParameters | |
}); | |
app.UseCookieAuthentication(new CookieAuthenticationOptions | |
{ | |
AutomaticAuthenticate = true, | |
AutomaticChallenge = true, | |
AuthenticationScheme = "Cookie", | |
CookieName = Configuration.GetSection("TokenAuthentication:CookieName").Value, | |
TicketDataFormat = new CustomJwtDataFormat( | |
SecurityAlgorithms.HmacSha256, | |
tokenValidationParameters) | |
}); | |
app.UseMiddleware<TokenProviderMiddleware>(Options.Create(tokenProviderOptions)); | |
} | |
private Task<ClaimsIdentity> GetIdentity(string username, string password) | |
{ | |
// DEMO CODE, DON NOT USE IN PRODUCTION!!! | |
if (username == "TEST" && password == "TEST123") | |
{ | |
return Task.FromResult(new ClaimsIdentity(new GenericIdentity(username, "Token"), new Claim[] { })); | |
} | |
// Account doesn't exists | |
return Task.FromResult<ClaimsIdentity>(null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment