Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs

163 lines
6.2 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authentication;
2026-06-01 13:42:40 +08:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Hosting;
2026-06-01 13:42:40 +08:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2026-06-01 13:42:40 +08:00
using Microsoft.IdentityModel.Tokens;
2026-06-30 09:15:52 +08:00
using QYZH.InteractiveMagazine.Infrastructure.Auth;
2026-06-01 13:42:40 +08:00
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
using QYZH.InteractiveMagazine.Models.Settings;
using System.Security.Claims;
2026-06-01 13:42:40 +08:00
using System.Text;
using System.Text.Encodings.Web;
2026-06-01 13:42:40 +08:00
namespace QYZH.InteractiveMagazine.Infrastructure.Extensions;
/// <summary>
2026-06-30 09:15:52 +08:00
/// Infrastructure service registration extensions.
2026-06-01 13:42:40 +08:00
/// </summary>
public static class DependencyInjectionExtensions
{
private const string NoAuthScheme = "NoAuth";
private const string DevelopmentAuthScheme = "DevelopmentSmartAuth";
2026-06-01 13:42:40 +08:00
/// <summary>
2026-06-30 09:15:52 +08:00
/// Registers infrastructure services.
2026-06-01 13:42:40 +08:00
/// </summary>
public static void AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
2026-06-01 13:42:40 +08:00
{
AddJwtAuthentication(services, configuration, environment);
2026-06-01 13:42:40 +08:00
services.AddTransient<GlobalExceptionMiddleware>();
services.AddTransient<OperationLogMiddleware>();
services.AddScoped<OperationLogActionFilter>();
2026-06-01 13:42:40 +08:00
}
private static void AddJwtAuthentication(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
2026-06-01 13:42:40 +08:00
{
var jwtSettings = configuration.GetSection("JwtSettings").Get<JwtSettings>()!;
services.AddSingleton(jwtSettings);
if (environment?.IsDevelopment() == true)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = DevelopmentAuthScheme;
options.DefaultChallengeScheme = DevelopmentAuthScheme;
})
.AddPolicyScheme(DevelopmentAuthScheme, null, options =>
{
options.ForwardDefaultSelector = context =>
{
var authHeader = context.Request.Headers.Authorization.FirstOrDefault();
return !string.IsNullOrWhiteSpace(authHeader) &&
authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)
? JwtBearerDefaults.AuthenticationScheme
: NoAuthScheme;
};
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => ConfigureJwtBearer(options, jwtSettings))
.AddScheme<AuthenticationSchemeOptions, NoAuthHandler>(NoAuthScheme, options => { });
return;
}
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => ConfigureJwtBearer(options, jwtSettings));
}
2026-06-01 13:42:40 +08:00
private static void ConfigureJwtBearer(JwtBearerOptions options, JwtSettings jwtSettings)
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtSettings.Issuer,
ValidateAudience = true,
ValidAudience = jwtSettings.Audience,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey!)),
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
2026-06-01 13:42:40 +08:00
options.Events = new JwtBearerEvents
{
OnTokenValidated = async context =>
2026-06-01 13:42:40 +08:00
{
var currentToken = GetBearerToken(context);
if (string.IsNullOrEmpty(currentToken))
2026-06-01 13:42:40 +08:00
{
context.Fail("Invalid token");
return;
}
var userId = context.Principal?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userId))
{
context.Fail("Invalid token");
return;
}
2026-06-30 09:15:52 +08:00
var wxUserId = context.Principal?.FindFirst(JwtHelper.WxUserIdClaimType)?.Value;
if (string.IsNullOrEmpty(wxUserId))
{
var adminToken = await RedisHelper.GetAsync(JwtHelper.BuildAdminTokenKey(userId));
if (adminToken == currentToken)
{
return;
}
context.Fail("Token expired, please login again");
return;
}
var wechatToken = await RedisHelper.GetAsync(JwtHelper.BuildWeChatTokenKey(wxUserId, userId));
if (wechatToken == currentToken)
{
return;
}
context.Fail("Token expired, please login again");
}
};
2026-06-01 13:42:40 +08:00
}
2026-06-30 09:15:52 +08:00
private static string? GetBearerToken(TokenValidatedContext context)
{
var authHeader = context.HttpContext.Request.Headers.Authorization.FirstOrDefault();
if (string.IsNullOrWhiteSpace(authHeader) || !authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
return null;
}
return authHeader.Substring("Bearer ".Length).Trim();
}
2026-06-01 13:42:40 +08:00
}
/// <summary>
2026-06-30 09:15:52 +08:00
/// Authentication handler used only in development.
/// </summary>
public class NoAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public NoAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var claims = new[]
{
2026-06-30 09:15:52 +08:00
new Claim(ClaimTypes.Name, "DevUser"),
new Claim(ClaimTypes.NameIdentifier, "0")
};
2026-06-30 09:15:52 +08:00
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}