Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public IHostingEnvironment Environment {get; set;}
public void ConfigureServices(IServiceCollection services)
{
var signingKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(
Configuration["Token:SigningKey"]);
var validationParams = new TokenValidationParameters()
{
ClockSkew = TimeSpan.Zero,
ValidateAudience = true,
ValidAudience = Configuration["Token:Audience"],
ValidateIssuer = true,
ValidIssuer = Configuration["Token:Issuer"],
IssuerSigningKey = signingKey,
ValidateIssuerSigningKey = true,
RequireExpirationTime = true,
ValidateLifetime = true
};
services.AddDataProtection(options =>
options.ApplicationDiscriminator = $"{Environment.ApplicationName}")
.SetApplicationName($"{Environment.ApplicationName}");
services.AddScoped<IDataSerializer,
TicketSerializer>();
services.AddScoped(serviceProvider =>
new JwtTokenGenerator(validationParams.ToTokenOptions()));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme =
CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme =
CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme =
CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.Expiration = TimeSpan.FromMinutes(5);
options.TicketDataFormat = new JwtAuthTicketFormat(validationParams,
services
.BuildServiceProvider()
.GetService<IDataSerializer>(),
services
.BuildServiceProvider()
.GetDataProtector(new[] { $"{Environment.ApplicationName}-Auth1" }));
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = options.LoginPath;
options.ReturnUrlParameter = "returnUrl";
});
services.AddMvc();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment