Skip to content

Commit 6081981

Browse files
committed
Alias功能合并到OAuthTokenAttribute
1 parent 042a30c commit 6081981

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

WebApiClientCore.Extensions.OAuths/Attributes/AliasOAuthTokenAttribute.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

WebApiClientCore.Extensions.OAuths/Attributes/OAuthTokenAttribute.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using System;
23
using System.Net;
34
using System.Net.Http.Headers;
45
using System.Threading.Tasks;
@@ -16,6 +17,11 @@ namespace WebApiClientCore.Attributes
1617
/// </remarks>
1718
public class OAuthTokenAttribute : ApiFilterAttribute
1819
{
20+
/// <summary>
21+
/// 获取或设置指定TokenProvider别名的方法参数名
22+
/// </summary>
23+
public string? AliasParameterName { get; set; }
24+
1925
/// <summary>
2026
/// 获取或设置token提供者的查找模式
2127
/// </summary>
@@ -53,8 +59,21 @@ public sealed override Task OnResponseAsync(ApiResponseContext context)
5359
/// <returns></returns>
5460
protected virtual ITokenProvider GetTokenProvider(ApiRequestContext context)
5561
{
62+
var alias = string.Empty;
63+
if (string.IsNullOrEmpty(this.AliasParameterName) == false)
64+
{
65+
if (context.TryGetArgument<string>(this.AliasParameterName, StringComparer.OrdinalIgnoreCase, out var aliasValue))
66+
{
67+
alias = aliasValue;
68+
}
69+
else
70+
{
71+
throw new InvalidOperationException($"未提供有效的参数值: {this.AliasParameterName}");
72+
}
73+
}
74+
5675
var factory = context.HttpContext.ServiceProvider.GetRequiredService<ITokenProviderFactory>();
57-
return factory.Create(context.ActionDescriptor.InterfaceType, this.TokenProviderSearchMode);
76+
return factory.Create(context.ActionDescriptor.InterfaceType, this.TokenProviderSearchMode, alias);
5877
}
5978

6079
/// <summary>

0 commit comments

Comments
 (0)