gpt4 book ai didi

c# - 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的承载身份验证

转载 作者:行者123 更新时间:2023-12-03 03:03:44 31 4
gpt4 key购买 nike

我正在尝试在 .NET Core 3 Preview 5 Web API 项目中从 Swashbuckle 版本 4.0.1 迁移到 5.0.0-rc2。

我已经完成了项目编译和 Swagger UI 的工作,但我无法让承载身份验证工作,我认为这是因为我没有正确设置新格式的安全性。

这是我在版本 4 中运行的旧代码:

c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});

var security = new Dictionary<string, IEnumerable<string>>
{
{"Bearer", new string[] { }},
};

c.AddSecurityRequirement(security);

这就是我在 v5 中将其更改为的内容:

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "tomsAuth"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "tomsAuth" }
}, new List<string>() }
});

我认为我的问题可能出在这部分代码中:

        new OpenApiSecurityScheme
{
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "tomsAuth" }
}, new List<string>() }

我认为该位可能应该在某个地方有“Bearer”,但我不确定在哪里?

其他信息

这就是我首先设置 JWT 身份验证的方式。此代码没有更改,并且在我使用 Swashbuckle 4.0.1 时可以正常工作:

    var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);

services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
var userService = context.HttpContext.RequestServices.GetRequiredService<IApiUserService>();
var userId = int.Parse(context.Principal.Identity.Name);
var user = userService.GetById(userId);
if (user == null)
{
// return unauthorized if user no longer exists
context.Fail("Unauthorized");
}

return Task.CompletedTask;
}
};
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});

最佳答案

通过反复试验最终使这个工作正常。这是适合我的代码:

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,

},
new List<string>()
}
});

我怀疑那里可能设置了一些实际上不需要显式设置的属性,但上面的内容对我有用。

关于c# - 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的承载身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234504/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com