ajout validation jeton jwt
This commit is contained in:
parent
c9b27393a0
commit
9399b6d92c
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -33,7 +33,7 @@
|
|||||||
"name": ".NET Core Launch (api)",
|
"name": ".NET Core Launch (api)",
|
||||||
"type": "coreclr",
|
"type": "coreclr",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"preLaunchTask": "build",
|
"preLaunchTask": "buildapi",
|
||||||
// If you have changed target frameworks, make sure to update the program path.
|
// 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",
|
"program": "${workspaceFolder}/Apps/HackerNet.Api/bin/Debug/net6.0/HackerNet.Api.dll",
|
||||||
"args": [],
|
"args": [],
|
||||||
|
|||||||
@ -4,19 +4,39 @@ using Microsoft.AspNetCore.Authentication;
|
|||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
var tokenValidation = builder.Configuration
|
||||||
|
.GetSection("TokenValidation")
|
||||||
|
.Get<TokenValidation>();
|
||||||
|
|
||||||
builder.Services.AddHackerNetServicesEntityFramework(builder.Configuration);
|
builder.Services.AddHackerNetServicesEntityFramework(builder.Configuration);
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services
|
builder.Services
|
||||||
.AddIdentityCore<IdentityUser>()
|
.AddIdentityCore<IdentityUser>(o =>
|
||||||
|
{
|
||||||
|
o.Password.RequireNonAlphanumeric = false;
|
||||||
|
})
|
||||||
.AddRoles<IdentityRole>()
|
.AddRoles<IdentityRole>()
|
||||||
.AddSignInManager()
|
.AddSignInManager()
|
||||||
.AddEntityFrameworkStores<HackerContext>();
|
.AddEntityFrameworkStores<HackerContext>();
|
||||||
|
|
||||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
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 =>
|
builder.Services.AddOpenApiDocument(d =>
|
||||||
{
|
{
|
||||||
@ -37,3 +57,10 @@ app.MapGet("/", () => "Hello World!");
|
|||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
public class TokenValidation
|
||||||
|
{
|
||||||
|
public string Audience { get; set; }
|
||||||
|
public string Issuer { get; set; }
|
||||||
|
public string Key { get; set; }
|
||||||
|
}
|
||||||
@ -31,5 +31,6 @@ POST {{url}}/api/accounts
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
|
"username": "test",
|
||||||
|
"password": "G6:c`bzr2h#Pq;4"
|
||||||
}
|
}
|
||||||
@ -7,5 +7,10 @@
|
|||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"Default": "Data Source=../HackerNet.Web/hackernet.db"
|
"Default": "Data Source=../HackerNet.Web/hackernet.db"
|
||||||
|
},
|
||||||
|
"TokenValidation": {
|
||||||
|
"Audience": "https://localhost:7252/",
|
||||||
|
"Issuer": "https://localhost:7252/",
|
||||||
|
"Key": "fwjlkf989r32lkf;wk"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user