70 lines
1.5 KiB
C#
70 lines
1.5 KiB
C#
![]() |
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|||
|
using Microsoft.IdentityModel.Tokens;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
var builder = WebApplication.CreateBuilder(args);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
var domain = builder.Configuration["Auth0:Domain"];
|
|||
|
var audience =builder.Configuration["Auth0:Audience"];
|
|||
|
|
|||
|
|
|||
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|||
|
.AddJwtBearer(options =>
|
|||
|
{
|
|||
|
options.Authority = domain;
|
|||
|
options.Audience = audience;
|
|||
|
options.TokenValidationParameters = new TokenValidationParameters
|
|||
|
{
|
|||
|
ValidateIssuer = true,
|
|||
|
ValidateAudience = true,
|
|||
|
ValidateLifetime = true,
|
|||
|
ValidIssuer = domain,
|
|||
|
ValidAudience = audience
|
|||
|
};
|
|||
|
});
|
|||
|
builder.Services.AddAuthorization();
|
|||
|
|
|||
|
|
|||
|
|
|||
|
builder.Services.AddControllers();
|
|||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|||
|
builder.Services.AddEndpointsApiExplorer();
|
|||
|
builder.Services.AddSwaggerGen();
|
|||
|
|
|||
|
var app = builder.Build();
|
|||
|
|
|||
|
// Configure the HTTP request pipeline.
|
|||
|
if (app.Environment.IsDevelopment())
|
|||
|
{
|
|||
|
app.UseSwagger();
|
|||
|
app.UseSwaggerUI();
|
|||
|
}
|
|||
|
|
|||
|
app.UseHttpsRedirection();
|
|||
|
|
|||
|
app.UseAuthentication();
|
|||
|
|
|||
|
app.UseAuthorization();
|
|||
|
|
|||
|
#pragma warning disable ASP0014 // Suggest using top level route registrations
|
|||
|
app.UseEndpoints(endpoints => endpoints.MapControllers());
|
|||
|
#pragma warning restore ASP0014 // Suggest using top level route registrations
|
|||
|
|
|||
|
|
|||
|
app.MapControllers();
|
|||
|
|
|||
|
app.Run();
|