Last active
September 16, 2019 23:27
-
-
Save utarn/af4f50bed878c24b42fd8c66cb038004 to your computer and use it in GitHub Desktop.
ASP.NET Core JWT Function part 2/2
This file contains hidden or 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
| // 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