Skip to content

Instantly share code, notes, and snippets.

@explorer14
Last active February 9, 2019 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save explorer14/9a445847f6ada0d2210eab44efeeb886 to your computer and use it in GitHub Desktop.
Save explorer14/9a445847f6ada0d2210eab44efeeb886 to your computer and use it in GitHub Desktop.
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