feat: 完成支持鉴权刷新功能

This commit is contained in:
ccnetcore
2025-06-29 19:34:09 +08:00
parent a33c6dbf1a
commit 3d22a2ef65
6 changed files with 107 additions and 27 deletions

View File

@@ -0,0 +1,64 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Yi.Framework.Core.Authentication;
namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Authentication;
/// <summary>
/// 可刷新的鉴权提供者
/// </summary>
public class RefreshAuthenticationHandlerProvider : IRefreshAuthenticationHandlerProvider
{
private Dictionary<string, IAuthenticationHandler> _handlerMap =
new Dictionary<string, IAuthenticationHandler>((IEqualityComparer<string>)StringComparer.Ordinal);
/// <summary>Constructor.</summary>
/// <param name="schemes">The <see cref="T:Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider" />.</param>
public RefreshAuthenticationHandlerProvider(IAuthenticationSchemeProvider schemes)
{
this.Schemes = schemes;
}
/// <summary>
/// The <see cref="T:Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider" />.
/// </summary>
public IAuthenticationSchemeProvider Schemes { get; }
/// <summary>Returns the handler instance that will be used.</summary>
/// <param name="context">The context.</param>
/// <param name="authenticationScheme">The name of the authentication scheme being handled.</param>
/// <returns>The handler instance.</returns>
public async Task<IAuthenticationHandler?> GetHandlerAsync(
HttpContext context,
string authenticationScheme)
{
IAuthenticationHandler handlerAsync;
if (this._handlerMap.TryGetValue(authenticationScheme, out handlerAsync))
return handlerAsync;
AuthenticationScheme schemeAsync = await this.Schemes.GetSchemeAsync(authenticationScheme);
if (schemeAsync == null)
return (IAuthenticationHandler)null;
if ((context.RequestServices.GetService(schemeAsync.HandlerType) ??
ActivatorUtilities.CreateInstance(context.RequestServices, schemeAsync.HandlerType)) is
IAuthenticationHandler handler)
{
handlerAsync = handler;
await handler.InitializeAsync(schemeAsync, context);
this._handlerMap[authenticationScheme] = handler;
}
return handlerAsync;
}
/// <summary>
/// 刷新鉴权
/// </summary>
public void RefreshAuthentication()
{
_handlerMap = new Dictionary<string, IAuthenticationHandler>((IEqualityComparer<string>)StringComparer.Ordinal);
}
}

View File

@@ -1,17 +1,12 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.Conventions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Options;
namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
{

View File

@@ -1,21 +1,10 @@
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Linq;
using Swashbuckle.AspNetCore.SwaggerGen;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.WebClientInfo;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Modularity;
using Yi.Framework.AspNetCore.Mvc;
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Authentication;
using Yi.Framework.Core;
using Yi.Framework.Core.Authentication;
namespace Yi.Framework.AspNetCore
{
@@ -35,8 +24,14 @@ namespace Yi.Framework.AspNetCore
// 替换默认的WebClientInfoProvider为支持代理的实现
services.Replace(new ServiceDescriptor(
typeof(IWebClientInfoProvider),
typeof(RealIpHttpContextWebClientInfoProvider),
typeof(RealIpHttpContextWebClientInfoProvider),
ServiceLifetime.Transient));
// 替换默认的AuthenticationHandlerProvider为支持刷新鉴权
services.Replace(new ServiceDescriptor(
typeof(IAuthenticationHandlerProvider),
typeof(RefreshAuthenticationHandlerProvider),
ServiceLifetime.Scoped));
}
}
}