如何正确配置 ASP.NET Core 5 Swagger 以使用 Azure A/D 授权码身份验证?
How to correctly configure ASP.NET Core 5 Swagger to work with Azure A/D authorization code authentication?
我正在将我的 ASP.NET Core 5 Web API 安全性从隐式升级到授权码。身份验证是使用 Azure A/D 完成的,我还需要允许使用 Swagger 文档。
我设法使它工作,但 client_secret 对我来说没用,我想知道我是否做错了什么。我的配置如下:
蔚蓝A/D
- 重定向 URI:https://localhost:44444/swagger/oauth2-redirect.html
- API 权限:Microsoft Graph + 自定义角色 (
access_as_user
)
- 令牌:访问令牌和 ID 令牌未选中
appsettings.json
"AzureAd": {
"Instance": "https://login.microsoftonline.com",
"ClientId": "xxxxxxxx-xxxxxxxxx",
"TenantId": "xxxxxxxx-xxxxxxxxx"
},
Startup.cs
services.AddMicrosoftIdentityWebApiAuthentication(Configuration);
private static void AddSwagger(IServiceCollection services, AzureAdAuthOptions auth)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Foo API", Version = "v1", });
AddSwaggerDocs_IncludeXmlComments(c);
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
AuthorizationCode = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{auth.Instance}/{auth.TenantId}/oauth2/v2.0/authorize"),
TokenUrl = new Uri($"{auth.Instance}/{auth.TenantId}/oauth2/v2.0/token"),
Scopes = { { $"api://{auth.ClientId}/access_as_user", "Access as user" } }
}
}
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
new string[] { }
}
});
});
}
招摇
- oauth2 (OAuth2, authorizationCode)
- 应用程序:Foo - Swagger
- 授权URL:https://login.microsoftonline.com//oauth2/v2.0/authorize
- 令牌URL:https://login.microsoftonline.com//oauth2/v2.0/令牌
- client_id: 已经填写
- client_secret
范围:只有一个,api://<guid>/access_as_user
如果我没有密码授权,它就可以正常工作。但是,如果我提供秘密,我会收到以下错误:
Auth ErrorError: Unauthorized, error: invalid_client, description:
AADSTS700025: Client is public so neither 'client_assertion' nor
'client_secret' should be presented. Trace ID:
dbb64172-2bbb-4392-8fc5-0ee71ab9c301 Correlation ID:
8c7edd8b-b6d0-435b-abeb-158b3278c3fe Timestamp: 2021-05-27 07:21:14Z
我以为需要这个秘密才能使身份验证过程更安全,但我似乎无法使用它。我做错了什么?
仔细检查,allowPublicClient
为假。我已经从 SPA 切换到 Web 客户端,提供了秘密,但我收到以下错误:
Auth ErrorError: Bad Request, error: invalid_request, description:
AADSTS9002326: Cross-origin token redemption is permitted only for the
'Single-Page Application' client-type. Trace ID:
6e11093f-319b-4d75-92b9-c6a39775a501 Correlation ID:
afcd9152-ccd5-4588-ae75-0b829272d66d Timestamp: 2021-05-28 06:10:58Z
所以,我似乎在使用 Swagger 时卡在了 SPA 客户端 UI。
此错误表明您的应用程序是 public 客户端应用程序,而不是机密客户端应用程序。请参阅 the differences between public client and confidential client applications。
解决这个问题,只需要修改应用清单,将allowPublicClient
改为false
。
此外,确保在创建应用程序时 select Web。
我正在将我的 ASP.NET Core 5 Web API 安全性从隐式升级到授权码。身份验证是使用 Azure A/D 完成的,我还需要允许使用 Swagger 文档。
我设法使它工作,但 client_secret 对我来说没用,我想知道我是否做错了什么。我的配置如下:
蔚蓝A/D
- 重定向 URI:https://localhost:44444/swagger/oauth2-redirect.html
- API 权限:Microsoft Graph + 自定义角色 (
access_as_user
) - 令牌:访问令牌和 ID 令牌未选中
appsettings.json
"AzureAd": {
"Instance": "https://login.microsoftonline.com",
"ClientId": "xxxxxxxx-xxxxxxxxx",
"TenantId": "xxxxxxxx-xxxxxxxxx"
},
Startup.cs
services.AddMicrosoftIdentityWebApiAuthentication(Configuration);
private static void AddSwagger(IServiceCollection services, AzureAdAuthOptions auth)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Foo API", Version = "v1", });
AddSwaggerDocs_IncludeXmlComments(c);
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
AuthorizationCode = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{auth.Instance}/{auth.TenantId}/oauth2/v2.0/authorize"),
TokenUrl = new Uri($"{auth.Instance}/{auth.TenantId}/oauth2/v2.0/token"),
Scopes = { { $"api://{auth.ClientId}/access_as_user", "Access as user" } }
}
}
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
new string[] { }
}
});
});
}
招摇
- oauth2 (OAuth2, authorizationCode)
- 应用程序:Foo - Swagger
- 授权URL:https://login.microsoftonline.com//oauth2/v2.0/authorize
- 令牌URL:https://login.microsoftonline.com//oauth2/v2.0/令牌
- client_id: 已经填写
- client_secret
范围:只有一个,api://<guid>/access_as_user
如果我没有密码授权,它就可以正常工作。但是,如果我提供秘密,我会收到以下错误:
Auth ErrorError: Unauthorized, error: invalid_client, description: AADSTS700025: Client is public so neither 'client_assertion' nor 'client_secret' should be presented. Trace ID: dbb64172-2bbb-4392-8fc5-0ee71ab9c301 Correlation ID: 8c7edd8b-b6d0-435b-abeb-158b3278c3fe Timestamp: 2021-05-27 07:21:14Z
我以为需要这个秘密才能使身份验证过程更安全,但我似乎无法使用它。我做错了什么?
仔细检查,allowPublicClient
为假。我已经从 SPA 切换到 Web 客户端,提供了秘密,但我收到以下错误:
Auth ErrorError: Bad Request, error: invalid_request, description: AADSTS9002326: Cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. Trace ID: 6e11093f-319b-4d75-92b9-c6a39775a501 Correlation ID: afcd9152-ccd5-4588-ae75-0b829272d66d Timestamp: 2021-05-28 06:10:58Z
所以,我似乎在使用 Swagger 时卡在了 SPA 客户端 UI。
此错误表明您的应用程序是 public 客户端应用程序,而不是机密客户端应用程序。请参阅 the differences between public client and confidential client applications。
解决这个问题,只需要修改应用清单,将allowPublicClient
改为false
。
此外,确保在创建应用程序时 select Web。