1
1
using Microsoft . AspNetCore . Authentication ;
2
+ using Microsoft . Extensions . DependencyInjection ;
3
+ using Microsoft . IdentityModel . Tokens ;
2
4
using System ;
3
5
4
6
namespace MakingSense . AspNetCore . Authentication . SimpleToken
5
7
{
6
8
public static class SimpleTokenAuthenticationExtensions
7
9
{
8
10
public static AuthenticationBuilder AddSimpleTokenAuthentication ( this AuthenticationBuilder builder )
9
- => builder . AddSimpleTokenAuthentication ( SimpleTokenDefaults . AuthenticationScheme , _ => { } ) ;
11
+ => builder . AddSimpleTokenAuthentication ( SimpleTokenDefaults . AuthenticationScheme , null ) ;
10
12
11
13
public static AuthenticationBuilder AddSimpleTokenAuthentication ( this AuthenticationBuilder builder ,
12
14
Action < SimpleTokenAuthenticationOptions > configureOptions )
@@ -22,7 +24,28 @@ public static AuthenticationBuilder AddSimpleTokenAuthentication(this Authentica
22
24
string displayName ,
23
25
Action < SimpleTokenAuthenticationOptions > configureOptions )
24
26
{
25
- return builder . AddScheme < SimpleTokenAuthenticationOptions , SimpleTokenAuthenticationHandler > ( authenticationScheme , displayName , configureOptions ) ;
27
+ return builder . AddScheme < SimpleTokenAuthenticationOptions , SimpleTokenAuthenticationHandler > ( authenticationScheme , displayName ,
28
+ ( SimpleTokenAuthenticationOptions options ) => {
29
+ configureOptions ? . Invoke ( options ) ;
30
+
31
+ if ( options . SecurityTokenValidatorsFactory == null )
32
+ {
33
+ options . SecurityTokenValidatorsFactory = ( ) =>
34
+ {
35
+ // TODO: fix it because it is using app services, and it should use scope services,
36
+ // a work around could be:
37
+ // ```
38
+ // SecurityTokenValidatorsFactory = () =>
39
+ // {
40
+ // var context = builder.Services.BuildServiceProvider().GetService<IHttpContextAccessor>().HttpContext;
41
+ // return context.RequestServices.GetServices<ISecurityTokenValidator>();
42
+ // }
43
+ // ```
44
+ var serviceProvider = builder . Services . BuildServiceProvider ( ) ;
45
+ return serviceProvider . GetServices < ISecurityTokenValidator > ( ) ;
46
+ } ;
47
+ }
48
+ } ) ;
26
49
}
27
50
}
28
51
}
0 commit comments