File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
WebApiClientCore.Extensions.OAuths/Attributes Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ using Microsoft . Extensions . DependencyInjection ;
2+ using System ;
3+ using WebApiClientCore ;
4+ using WebApiClientCore . Attributes ;
5+ using WebApiClientCore . Extensions . OAuths ;
6+
7+ namespace App . Attributes
8+ {
9+ /// <summary>
10+ /// 表示支持多用户的token应用特性
11+ /// </summary>
12+ public class MultiUserOAuthTokenAttribute : OAuthTokenAttribute
13+ {
14+ /// <summary>
15+ /// 获取string类型的用户Id参数名
16+ /// </summary>
17+ public string UserIdParameterName { get ; }
18+
19+ /// <summary>
20+ /// 支持多用户的token应用特性
21+ /// </summary>
22+ /// <param name="userIdParameterName">string类型的用户Id参数名</param>
23+ public MultiUserOAuthTokenAttribute ( string userIdParameterName )
24+ {
25+ this . UserIdParameterName = userIdParameterName ;
26+ }
27+
28+ /// <summary>
29+ /// <inheritdoc></inheritdoc>
30+ /// </summary>
31+ /// <param name="context"></param>
32+ /// <returns></returns>
33+ /// <exception cref="InvalidOperationException"></exception>
34+ protected sealed override ITokenProvider GetTokenProvider ( ApiRequestContext context )
35+ {
36+ if ( context . TryGetArgument ( this . UserIdParameterName , StringComparer . OrdinalIgnoreCase , out var userId ) &&
37+ userId is string userIdValue )
38+ {
39+ var tokenProviderFactory = context . HttpContext . ServiceProvider . GetRequiredService < ITokenProviderFactory > ( ) ;
40+ return tokenProviderFactory . Create ( context . ActionDescriptor . InterfaceType , this . TokenProviderSearchMode , userIdValue ) ;
41+ }
42+
43+ throw new InvalidOperationException ( $ "未提供有效的参数值: { this . UserIdParameterName } ") ;
44+ }
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments