Skip to content

Commit 90f2f4d

Browse files
committed
修复添加认证的空指针
1 parent c8b9136 commit 90f2f4d

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/ZServer.API/JwtBearerAuthenticationExtensions.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)