[英]AllowAnonymous for retrieve token not working in Swagger UI on debug
不知何故,我們正在與我的同事合作的同一個項目不會調用 HTTP POST 方法來檢索我的 PC 上的令牌,但在我同事的 PC 上運行良好。
代碼是一樣的,但我的 output 顯示了這個彈出窗口:
該請求在 Postman 中運行良好:
測試過不同的瀏覽器。 我們已經在服務器上部署了 package 並且令牌被正確檢索。 在本地部署 package 顯示相同的錯誤。 我電腦上的某些東西會干擾 HTTP 請求嗎?
這是代碼:
services
.AddSwaggerGen(
(c) =>
{
var version = $"v{Assembly.GetEntryAssembly()?.GetVersionInfo()}";
c.OperationFilter<ChorusModeHeaderFilter>();
c.OperationFilter<AuthenticationFilter>();
c.OperationFilter<ErrorFilter>();
c.OperationFilter<MaintenanceFilter>();
c.SwaggerDoc(version, new OpenApiInfo { Title = appName, Version = version });
c.AddSecurityDefinition(
"Bearer",
new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OpenIdConnect,
OpenIdConnectUrl = new Uri($"../../.well-known/openid-configuration", UriKind.Relative)
});
c.AddSecurityRequirement(
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},
Array.Empty<string>()
}
});
xmlDocumentationFiles
.Select((path) => Path.Combine(AppContext.BaseDirectory, path))
.ToList()
.ForEach((path) => c.IncludeXmlComments(path));
});
並且:
public class AuthenticationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (!context.MethodInfo.GetCustomAttributes(true).OfType<AllowAnonymousAttribute>().Any())
{
operation.Responses.Add(
$"{(int)HttpStatusCode.Unauthorized}",
new OpenApiResponse { Description = "Unauthorized" });
}
else
{
// No authentication skip it
}
}
}
您可以將 startup.cs class 中的代碼發布到 API 中嗎? (請求管道?)看起來你已經添加了對 BasicAuthentication 的支持?
如果您的 API 應該接受令牌,您應該使用 AddJwtBearer。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.