From 9fe564d452af7d5023cc410cd26761ba05730dc3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 12 Dec 2020 15:58:55 +0200 Subject: Last push cleanup --- API/Extensions/ConfigureJWT.cs | 47 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'API/Extensions/ConfigureJWT.cs') diff --git a/API/Extensions/ConfigureJWT.cs b/API/Extensions/ConfigureJWT.cs index f5862f5..1de2aa5 100644 --- a/API/Extensions/ConfigureJWT.cs +++ b/API/Extensions/ConfigureJWT.cs @@ -1,21 +1,48 @@ -using Microsoft.AspNetCore.Builder; +using System.Text; +using System.Threading.Tasks; +using Data.Models.Options; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Models; +using Microsoft.IdentityModel.Tokens; namespace API.Extensions { public static class JWTExtensions { - public static void JWTConfiguration(this IServiceCollection services) + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) { - - } - - public static void UseJWTConfiguration(this IApplicationBuilder app) - { - - + services.AddSingleton( + new JWTOptions(configuration.GetSection("AppSettings").GetSection("Secret").Value)); + // Get key from appsettings.json + var key = Encoding.ASCII.GetBytes(configuration.GetSection("AppSettings").GetSection("Secret").Value); + // Setup Jwt Authentication + services.AddAuthentication(x => + { + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(x => + { + x.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + // TODO: add more authentication + return Task.CompletedTask; + } + }; + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; + }); } } } \ No newline at end of file -- cgit v1.2.3