66 lines
2.6 KiB
C#
66 lines
2.6 KiB
C#
using FASS.Scheduler.Models;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
|
|
namespace FASS.Scheduler.Extensions.Configure
|
|
{
|
|
public static class AuthExtension
|
|
{
|
|
public static IServiceCollection AddAuth(this IServiceCollection services, AppSettings appSettings)
|
|
{
|
|
services
|
|
.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters()
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidIssuer = appSettings.Auth.Issuer,
|
|
|
|
ValidateAudience = true,
|
|
ValidAudience = appSettings.Auth.Audience,
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Auth.SigningKey)),
|
|
|
|
ValidateLifetime = true,
|
|
RequireExpirationTime = true,
|
|
ClockSkew = TimeSpan.Zero
|
|
};
|
|
options.Events = new JwtBearerEvents()
|
|
{
|
|
OnMessageReceived = context =>
|
|
{
|
|
if (context.Request.Headers.ContainsKey("Authorization"))
|
|
{
|
|
context.Token = context.Request.Headers["Authorization"].FirstOrDefault()?.Substring("Bearer ".Length);
|
|
}
|
|
return Task.CompletedTask;
|
|
},
|
|
OnAuthenticationFailed = context =>
|
|
{
|
|
if (context.Exception is SecurityTokenExpiredException)
|
|
{
|
|
context.Response.Headers.Append("Token-Expired", "true");
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
});
|
|
return services;
|
|
}
|
|
|
|
public static IApplicationBuilder UseAuth(this IApplicationBuilder app)
|
|
{
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
return app;
|
|
}
|
|
}
|
|
}
|