feat: jwt application
This commit is contained in:
20
Jwt.Application/Jwt.Application.csproj
Normal file
20
Jwt.Application/Jwt.Application.csproj
Normal 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>
|
||||
6
Jwt.Application/Jwt.Application.http
Normal file
6
Jwt.Application/Jwt.Application.http
Normal file
@@ -0,0 +1,6 @@
|
||||
@Jwt.Application_HostAddress = http://localhost:5064
|
||||
|
||||
GET {{Jwt.Application_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
76
Jwt.Application/Program.cs
Normal file
76
Jwt.Application/Program.cs
Normal 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();
|
||||
41
Jwt.Application/Properties/launchSettings.json
Normal file
41
Jwt.Application/Properties/launchSettings.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Jwt.Application/appsettings.Development.json
Normal file
8
Jwt.Application/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Jwt.Application/appsettings.json
Normal file
9
Jwt.Application/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user