54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
|
using Metrolog_API.Services;
|
|||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|||
|
using Microsoft.IdentityModel.Tokens;
|
|||
|
using System.Security.Cryptography;
|
|||
|
using System.Text;
|
|||
|
using Metrolog_API.Static;
|
|||
|
|
|||
|
var builder = WebApplication.CreateBuilder(args);
|
|||
|
|
|||
|
// Add services to the container.
|
|||
|
|
|||
|
builder.Services.AddControllers();
|
|||
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|||
|
builder.Services.AddOpenApi();
|
|||
|
|
|||
|
//add authentication JWT configuration
|
|||
|
string? JwtSecret = System.Environment.GetEnvironmentVariable("JWT_SECRET");
|
|||
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|||
|
.AddJwtBearer(options =>
|
|||
|
{
|
|||
|
options.TokenValidationParameters = new TokenValidationParameters
|
|||
|
{
|
|||
|
ValidateIssuerSigningKey = true,
|
|||
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtSecret)),
|
|||
|
ValidateIssuer = false, // issuer verify
|
|||
|
ValidateAudience = false, // Audience verify
|
|||
|
ValidateLifetime = true // Token expiration verify
|
|||
|
};
|
|||
|
});
|
|||
|
|
|||
|
builder.Services.AddAuthorization();
|
|||
|
|
|||
|
//add services
|
|||
|
builder.Services.AddSingleton<UserService>();
|
|||
|
builder.Services.AddSingleton<RoleService>();
|
|||
|
builder.Services.AddScoped<PasswordService>();
|
|||
|
builder.Services.AddSingleton<JWTService>();
|
|||
|
|
|||
|
var app = builder.Build();
|
|||
|
|
|||
|
// Configure the HTTP request pipeline.
|
|||
|
if (app.Environment.IsDevelopment())
|
|||
|
{
|
|||
|
app.MapOpenApi();
|
|||
|
}
|
|||
|
|
|||
|
app.UseAuthentication();
|
|||
|
app.UseAuthorization();
|
|||
|
|
|||
|
app.MapControllers();
|
|||
|
|
|||
|
app.Run();
|
|||
|
|