gpt4 book ai didi

c# - swagger oauth 安全定义

转载 作者:行者123 更新时间:2023-12-03 05:50:58 25 4
gpt4 key购买 nike

在我当前的 webapi 项目中,我设置了一个带有隐式流程和授权 url 的 swagger oauth 安全定义 https://login.microsoftonline.com/ + 租户 ID。范围与 Swashbuckle.AspNetCore nuget 的 github exapmle 中的范围相同,这是链接 https://github.com/domaindrivendev/Swashbuckle.AspNetCore 。但是当我尝试在 swagger 在线编辑器上进行身份验证时,这个 https://editor.swagger.io/ ,我无法取回 token 并收到 404 异常。我需要在我的 Azure 门户注册应用程序中设置什么才能将 token 返回到在线 swagger 编辑器?

最佳答案

根据您的描述,我创建了 .Net Core 2.0 Web API 应用程序,并在 Azure 门户上创建了 AAD 应用程序。 ConfigureServices 下的配置如下所示:

services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// Configure the app to use Jwt Bearer Authentication
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = string.Format(CultureInfo.InvariantCulture, "https://sts.windows.net/{0}/", Configuration["AzureAd:TenantId"]);
jwtOptions.Audience = Configuration["AzureAd:WebApiApp:ClientId"];
});

对于 Swagger UI,我还在 Azure 门户上创建了一个新的 AAD 应用程序,并添加了访问 Web API 应用程序的权限,如下所示:

enter image description here

然后,我添加了以下代码片段来定义 OAuth2.0 方案,如下所示:

// Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = string.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}/oauth2/authorize", Configuration["AzureAd:TenantId"]),
Scopes = new Dictionary<string, string>
{
{ "user_impersonation", "Access Bruce-WebAPI-NetCore" }
}
});
// Enable operation filter based on AuthorizeAttribute
c.OperationFilter<SecurityRequirementsOperationFilter>();

并使用以下代码初始化中间件以服务 swagger-ui。

// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.ConfigureOAuth2(
Configuration["AzureAd:SwaggerApp:ClientId"],
Configuration["AzureAd:SwaggerApp:ClientSecret"],
Configuration["AzureAd:SwaggerApp:RedirectUri"], //http://localhost:30504/swagger/o2c.html
"Bruce-WebAPI-NetCore-Swagger",
additionalQueryStringParameters: new Dictionary<string, string>(){
{ "resource",Configuration["AzureAd:WebApiApp:ClientId"]}
});
});

测试:

enter image description here

enter image description here

but it still returns AADSTS50001 Resource identifier is not provided

在处理过程中,我遇到了类似的问题。最后我发现resource未指定参数。然后,我设置了additionalQueryStringParameters ConfigureOAuth2 的参数。这是我的代码示例 WebAPI-Swagger-NetCore ,你可以引用一下。

此外,要向您的资源应用程序 (Web API) 添加访问范围,您可以按照 here 下的向您的资源应用程序添加访问范围部分进行操作。 。还有我的SecurityRequirementsOperationFilter没有将范围要求分配给基于 AuthorizeAttribute 的操作提供here 。您可以在 AddSecurityDefinition 下指定支持的范围,那么对于您的 Controller 或操作,您可以将其标记为 [Authorize(AuthenticationSchemes = "Bearer", Policy = "{scope}")] 。详情可以关注这个sample .

关于c# - swagger oauth 安全定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057672/

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