From 9399b6d92cf1461a4f35a4878e1f4492357ccad3 Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Wed, 15 Dec 2021 14:59:11 +0100 Subject: [PATCH] ajout validation jeton jwt --- .vscode/launch.json | 2 +- Apps/HackerNet.Api/Program.cs | 31 +++++++++++++++++-- Apps/HackerNet.Api/api.http | 3 +- .../appsettings.Development.json | 5 +++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a19c60a..9468f94 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -33,7 +33,7 @@ "name": ".NET Core Launch (api)", "type": "coreclr", "request": "launch", - "preLaunchTask": "build", + "preLaunchTask": "buildapi", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/Apps/HackerNet.Api/bin/Debug/net6.0/HackerNet.Api.dll", "args": [], diff --git a/Apps/HackerNet.Api/Program.cs b/Apps/HackerNet.Api/Program.cs index e620df4..5437b7b 100644 --- a/Apps/HackerNet.Api/Program.cs +++ b/Apps/HackerNet.Api/Program.cs @@ -4,19 +4,39 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); +var tokenValidation = builder.Configuration + .GetSection("TokenValidation") + .Get(); + builder.Services.AddHackerNetServicesEntityFramework(builder.Configuration); builder.Services.AddControllers(); builder.Services - .AddIdentityCore() + .AddIdentityCore(o => + { + o.Password.RequireNonAlphanumeric = false; + }) .AddRoles() .AddSignInManager() .AddEntityFrameworkStores(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) - .AddJwtBearer(); + .AddJwtBearer(o => + { + o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters + { + ValidAudience = tokenValidation.Audience, + ValidIssuer = tokenValidation.Issuer, + ValidateAudience = true, + ValidateIssuer = true, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(tokenValidation.Key)), + }; + }); builder.Services.AddOpenApiDocument(d => { @@ -37,3 +57,10 @@ app.MapGet("/", () => "Hello World!"); app.MapControllers(); app.Run(); + +public class TokenValidation +{ + public string Audience { get; set; } + public string Issuer { get; set; } + public string Key { get; set; } +} \ No newline at end of file diff --git a/Apps/HackerNet.Api/api.http b/Apps/HackerNet.Api/api.http index d026f37..f961ce5 100644 --- a/Apps/HackerNet.Api/api.http +++ b/Apps/HackerNet.Api/api.http @@ -31,5 +31,6 @@ POST {{url}}/api/accounts Content-Type: application/json { - + "username": "test", + "password": "G6:c`bzr2h#Pq;4" } \ No newline at end of file diff --git a/Apps/HackerNet.Api/appsettings.Development.json b/Apps/HackerNet.Api/appsettings.Development.json index e3873b5..c926728 100644 --- a/Apps/HackerNet.Api/appsettings.Development.json +++ b/Apps/HackerNet.Api/appsettings.Development.json @@ -7,5 +7,10 @@ }, "ConnectionStrings": { "Default": "Data Source=../HackerNet.Web/hackernet.db" + }, + "TokenValidation": { + "Audience": "https://localhost:7252/", + "Issuer": "https://localhost:7252/", + "Key": "fwjlkf989r32lkf;wk" } }