使用 swagger ui 的 Azure Functions Ad 身份验证说找不到 redirect.html
Azure Functions Ad authentication using swagger ui says redirect.html is not found
我正在使用 Azure Functions V3
这是我的开始 up.cs
public override void Configure(IFunctionsHostBuilder builder)
{
var configuration = builder.GetContext().Configuration;
builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), opts =>
{
opts.SpecVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
opts.Title = "My test app";
opts.ConfigureSwaggerGen = x =>
{
//custom operation example
x.CustomOperationIds(apiDesc => apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)
? methodInfo.Name
: new Guid().ToString());
//custom filter example
//x.DocumentFilter<RemoveSchemasFilter>();
//oauth2
x.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/authorize", configuration["AzureAd:TenantId"])),
Scopes = new Dictionary<string, string>
{
{ configuration["AzureAd:scope"], "scope" }
}
},
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/authorize", configuration["AzureAd:TenantId"])),
TokenUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", configuration["AzureAd:TenantId"])),
Scopes = new Dictionary<string, string>
{
{ configuration["AzureAd:scope"], "scope" }
}
}
}
});
x.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
},
Scheme = "oauth2",
Name = "oauth2",
In = ParameterLocation.Header
},
new List<string>()
}
});
};
opts.ClientId = configuration["AzureAd:ClientId"];
opts.OAuth2RedirectPath = "http://localhost:7071/api/swagger/ui/o2c-html";
//configuration["AzureAd:redirectURI"];
});
builder.Services.AddLogging();
}
大摇大摆Ui产生了。但是,当我单击授权时,它会重定向到 redirect.html 并说找不到。
This localhost page can't be found没有找到网址的网页:http://localhost:7071/api/swagger/ui/o2c-html#
感谢@useruser1672994 对此提供见解。添加这个解决了问题
/// <summary>
/// This is only needed for OAuth2 client. This redirecting document is normally served
/// as a static content. Functions don't provide this out of the box, so we serve it here.
/// Don't forget to set OAuth2RedirectPath configuration option to reflect this route.
/// </summary>
/// <param name="req"></param>
/// <param name="swashBuckleClient"></param>
/// <returns></returns>
[SwaggerIgnore]
[FunctionName("SwaggerOAuth2Redirect")]
public static Task<HttpResponseMessage> SwaggerOAuth2Redirect(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/oauth2-redirect")]
HttpRequestMessage req,
[SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
{
return Task.FromResult(swashBuckleClient.CreateSwaggerOAuth2RedirectResponse(req));
}
我正在使用 Azure Functions V3 这是我的开始 up.cs public override void Configure(IFunctionsHostBuilder builder) { var configuration = builder.GetContext().Configuration;
builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), opts =>
{
opts.SpecVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
opts.Title = "My test app";
opts.ConfigureSwaggerGen = x =>
{
//custom operation example
x.CustomOperationIds(apiDesc => apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)
? methodInfo.Name
: new Guid().ToString());
//custom filter example
//x.DocumentFilter<RemoveSchemasFilter>();
//oauth2
x.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/authorize", configuration["AzureAd:TenantId"])),
Scopes = new Dictionary<string, string>
{
{ configuration["AzureAd:scope"], "scope" }
}
},
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/authorize", configuration["AzureAd:TenantId"])),
TokenUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", configuration["AzureAd:TenantId"])),
Scopes = new Dictionary<string, string>
{
{ configuration["AzureAd:scope"], "scope" }
}
}
}
});
x.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
},
Scheme = "oauth2",
Name = "oauth2",
In = ParameterLocation.Header
},
new List<string>()
}
});
};
opts.ClientId = configuration["AzureAd:ClientId"];
opts.OAuth2RedirectPath = "http://localhost:7071/api/swagger/ui/o2c-html";
//configuration["AzureAd:redirectURI"];
});
builder.Services.AddLogging();
}
大摇大摆Ui产生了。但是,当我单击授权时,它会重定向到 redirect.html 并说找不到。 This localhost page can't be found没有找到网址的网页:http://localhost:7071/api/swagger/ui/o2c-html#
感谢@useruser1672994 对此提供见解。添加这个解决了问题
/// <summary>
/// This is only needed for OAuth2 client. This redirecting document is normally served
/// as a static content. Functions don't provide this out of the box, so we serve it here.
/// Don't forget to set OAuth2RedirectPath configuration option to reflect this route.
/// </summary>
/// <param name="req"></param>
/// <param name="swashBuckleClient"></param>
/// <returns></returns>
[SwaggerIgnore]
[FunctionName("SwaggerOAuth2Redirect")]
public static Task<HttpResponseMessage> SwaggerOAuth2Redirect(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/oauth2-redirect")]
HttpRequestMessage req,
[SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
{
return Task.FromResult(swashBuckleClient.CreateSwaggerOAuth2RedirectResponse(req));
}