【问题标题】:Swagger UI v6.4.0 AllowAnonymous for retrieve token not working on debugSwagger UI v6.4.0 AllowAnonymous 用于检索令牌在调试中不起作用
【发布时间】:2022-09-30 22:52:06
【问题描述】:

不知何故,我们正在与我的同事合作的同一个项目没有调用在我的电脑上检索令牌的 http post 方法,但在我的同事电脑上工作正常。 代码是相同的,但我的输出显示了这个弹出窗口。

在我的邮递员请求中也可以正常工作

已经测试了不同的浏览器,我们已经在服务器上部署了包,并且正确地检索了令牌。在本地部署包显示相同的错误,所以我的电脑干扰了 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
        }
    }
}

    标签: asp.net postman token openid-connect swagger-ui


    【解决方案1】:

    您可以在 API 中发布您的 startup.cs 类中的代码吗? (请求管道?)看起来您添加了对 BasicAuthentication 的支持?

    如果您的 API 应该接受令牌,则应该使用 AddJwtBearer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多