PlayerManagement/Api/Configurations/ServiceExtensions.cs
Tomasi - Developing a8a032c1d7 ..
2024-10-10 07:43:07 +02:00

59 lines
2.0 KiB
C#

using System.Text;
using Asp.Versioning;
using Database;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
namespace Api.Configurations;
public static class ServiceExtensions
{
public static void ConfigureAndAddCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll", p => p
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin());
});
}
public static void ConfigureAndApiVersioning(this IServiceCollection services)
{
services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});
}
public static void ConfigureAndAddHealthChecks(this IServiceCollection services, IConfiguration configuration)
{
services.AddHealthChecks()
.AddSqlServer(configuration.GetConnectionString("ApplicationDbConnection")!)
.AddDbContextCheck<ApplicationContext>();
}
public static void ConfigureAndAddAuthentication(this IServiceCollection services, IConfigurationSection jwtSection)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSection["Issuer"],
ValidAudience = jwtSection["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSection["Key"]!))
};
});
}
}