You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Azure Function App URL访问Swagger UI时遇未授权错误

Azure Function App访问Swagger UI时出现未授权错误

本地运行Swagger功能正常,但通过Azure URL访问时Swagger UI无法加载,且遇到未授权错误,错误截图如下:
未授权错误截图

Startup类代码

[assembly: FunctionsStartup(typeof(Startup))]
namespace ADPScheduleRequestService
{
    [ExcludeFromCodeCoverage]
    internal class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            //Register the extension
            builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), opts =>
            {
                opts.SpecVersion = OpenApiSpecVersion.OpenApi3_0;
                opts.AddCodeParameter = true;
                opts.PrependOperationWithRoutePrefix = true;
                //opts.XmlPath = "TestFunction.xml";
                opts.Documents = new[]
                {
                    new SwaggerDocument
                    {
                        Name = "v1",
                        Title = "ADP Schedule Request Service",
                        Version = "v2",                       
                    }
                };
                opts.Title = "ADP";
                opts.ConfigureSwaggerGen = x =>
                {
                    //x.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
                    x.CustomOperationIds(apiDesc => apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)
                        ? methodInfo.Name
                        : new Guid().ToString());
                };
            });
        }
    }
}

Swagger Functions类代码

namespace ADPScheduleRequestService
{
    [ExcludeFromCodeCoverage]
    public static class SwaggerFunctions
    {
        [SwaggerIgnore]
        [FunctionName("Swagger")]
        public static Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/swagger/json")]
            HttpRequestMessage req,
            [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
        {
            return Task.FromResult(swashBuckleClient.CreateSwaggerJsonDocumentResponse(req));
        }

        [SwaggerIgnore]
        [FunctionName("SwaggerUi")]
        public static Task<HttpResponseMessage> Run2(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/swagger/ui")]
            HttpRequestMessage req,
            [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
        {
            return Task.FromResult(swashBuckleClient.CreateSwaggerUIResponse(req, "v1/swagger/json"));
        }

    }
}

内容的提问来源于stack exchange,提问作者Saksham Srivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:03:30