@@ -26,15 +26,18 @@ public static class JwtBearerAuthenticationExtensions
2626 public static AuthenticationBuilder AddAuthentication ( this IServiceCollection services ,
2727 IConfiguration configuration , string apiName )
2828 {
29+ var builder = services . AddAuthentication ( ) ;
30+
2931 var jwtBearerOptions = configuration . GetSection ( "JwtBearer" ) . Get < JwtBearerSettings > ( ) ;
30- var rsaSecurityKey = jwtBearerOptions . GetRsaSecurityKey ( ) ;
31- if ( rsaSecurityKey != null )
32+ if ( jwtBearerOptions != null )
3233 {
33- services . AddKeyedSingleton ( JwtBearerSettings . JwtBearerRsaSecurityKey , rsaSecurityKey ) ;
34- }
34+ var rsaSecurityKey = jwtBearerOptions . GetRsaSecurityKey ( ) ;
35+ if ( rsaSecurityKey != null )
36+ {
37+ services . AddKeyedSingleton ( JwtBearerSettings . JwtBearerRsaSecurityKey , rsaSecurityKey ) ;
38+ }
3539
36- var builder = services . AddAuthentication ( ) ;
37- builder . AddJwtBearer ( options =>
40+ builder . AddJwtBearer ( options =>
3841 {
3942 options . Events = new JwtBearerEvents
4043 {
@@ -70,6 +73,7 @@ public static AuthenticationBuilder AddAuthentication(this IServiceCollection se
7073 options . MetadataAddress = jwtBearerOptions . GetMetadataAddress ( ) ;
7174 }
7275 } ) ;
76+ }
7377
7478 var tokens = configuration . GetSection ( "tokens" ) . Get < HashSet < string > > ( ) ;
7579 builder . AddScheme < TokenAuthOptions , TokenAuthHandler > ( "Token" ,
0 commit comments