feat: jwt application

This commit is contained in:
2024-10-13 14:56:22 +07:00
parent a934952816
commit e9f32fc32e
11 changed files with 295 additions and 4 deletions

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6"/>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RsaKeyLoader\RsaKeyLoader.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@Jwt.Application_HostAddress = http://localhost:5064
GET {{Jwt.Application_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,76 @@
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
var jwKey = await new HttpClient().GetStringAsync("https://localhost:5000/jwk");
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddCors();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthentication()
.AddJwtBearer(b =>
{
b.TokenValidationParameters = new TokenValidationParameters()
{
// для облегчения дебага
ValidateAudience = false,
ValidateIssuer = false,
};
// important
b.Configuration = new OpenIdConnectConfiguration()
{
SigningKeys =
{
JsonWebKey.Create(jwKey)
}
};
// b.Events = new JwtBearerEvents()
// {
// OnMessageReceived = (ctx) =>
// {
// if (ctx.Request.Query.ContainsKey("token"))
// {
// ctx.Token = ctx.Request.Query["token"];
// }
//
// return Task.CompletedTask;
// }
// };
});
builder.Services.AddAuthorization();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors(p =>
{
p.AllowAnyOrigin();
p.AllowAnyMethod();
p.AllowAnyHeader();
});
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/me", (HttpContext ctx) =>
{
return ctx.User.FindFirst("name").Value;
})
.RequireAuthorization()
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:2141",
"sslPort": 44353
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7000;http://localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}