|
|
|
@ -0,0 +1,73 @@
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Infrastructure.Options;
|
|
|
|
|
using Infrastructure.Security;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
|
|
|
|
namespace Infrastructure.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class AuthenticationSetup
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 配置认证服务,包含jwt认证
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
/// <param name="builderOptions">自定义认证服务配置</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IServiceCollection AddDefaultAuthentication(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
Action<AuthenticationBuilder>? builderOptions = null)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
|
var audienceOptions = configuration.GetSection(AudienceOptions.Name).Get<AudienceOptions>();
|
|
|
|
|
if (audienceOptions is null || !audienceOptions.IsEnable)
|
|
|
|
|
{
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<JwtSecurityTokenHandler>();
|
|
|
|
|
services.AddScoped<IUserContext, UserContext>();
|
|
|
|
|
services.AddSingleton<DefaultTokenHandler>();
|
|
|
|
|
services.AddSingleton<IEncryptionService, EncryptionService>();
|
|
|
|
|
services.AddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsPostConfigureOptions>();
|
|
|
|
|
services.AddSingleton<ITokenBuilder, TokenBuilder>();
|
|
|
|
|
|
|
|
|
|
var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(key);
|
|
|
|
|
var buffer = Encoding.UTF8.GetBytes(key);
|
|
|
|
|
var securityKey = new SymmetricSecurityKey(buffer);
|
|
|
|
|
|
|
|
|
|
var tokenValidationParameters = new TokenValidationParameters()
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
IssuerSigningKey = securityKey,
|
|
|
|
|
ValidIssuer = audienceOptions.Issuer,
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidAudience = audienceOptions.Audience,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ClockSkew = TimeSpan.FromSeconds(300),
|
|
|
|
|
RequireExpirationTime = true,
|
|
|
|
|
RoleClaimType = ClaimTypes.Role
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var builder = services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = nameof(DefaultAuthenticationHandler);
|
|
|
|
|
options.DefaultForbidScheme = nameof(DefaultAuthenticationHandler);
|
|
|
|
|
});
|
|
|
|
|
builder.AddScheme<AuthenticationSchemeOptions, DefaultAuthenticationHandler>(
|
|
|
|
|
nameof(DefaultAuthenticationHandler),
|
|
|
|
|
options => { });
|
|
|
|
|
builder.AddJwtBearer(options => { options.TokenValidationParameters = tokenValidationParameters; });
|
|
|
|
|
builderOptions?.Invoke(builder);
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|