Skip to content

Instantly share code, notes, and snippets.

@utarn
Last active September 16, 2019 23:27
Show Gist options
  • Select an option

  • Save utarn/af4f50bed878c24b42fd8c66cb038004 to your computer and use it in GitHub Desktop.

Select an option

Save utarn/af4f50bed878c24b42fd8c66cb038004 to your computer and use it in GitHub Desktop.
ASP.NET Core JWT Function part 2/2
// appSettings.json
"Jwt": {
"Key": "secret-key",
"Issuer": "https://localhost:5000/"
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
services.AddMvc(options =>
{
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
})
.AddJsonOptions(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.Formatting = Formatting.Indented;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory, IServiceProvider provider, IConfiguration configuration)
{
app.UseAuthentication();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment