我正在尝试使用Azure AD身份验证和Swagger设置.NET Core Web API,但在尝试进行身份验证时会出现错误。
我正在遵循本指南:http://www.sharepointconfig.com/2018/08/configure-swagger-to-authenticate-against-azure-ad/
ConfigureServices包含以下代码:
services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("oauth2", //Name the security scheme
new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows() { Implicit = new OpenApiOAuthFlow() },
Scheme = "oauth2",
OpenIdConnectUrl = new Uri($"https://login.microsoftonline.com/" + _configuration["AzureAD:TenantId"] + "/oauth2/authorize"),
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement{
{
new OpenApiSecurityScheme{
Reference = new OpenApiReference{
Id = "oauth2",
Type = ReferenceType.SecurityScheme
}
},new List<string>()
}
});
});
配置包含此代码:
app.UseSwaggerUI(c =>
{
c.OAuthClientId(_configuration["Swagger:ClientId"]);
c.OAuthClientSecret(_configuration["Swagger:ClientSecret"]);
c.OAuthRealm(_configuration["AzureAD:ClientId"]);
c.OAuthAppName("WerfRegistratie V1");
c.OAuthScopeSeparator(" ");
c.OAuthAdditionalQueryStringParams(new Dictionary<string, string> { { "resource", _configuration["AzureAD:ClientId"] } });
});
我一直遇到的问题是这个:Swagger authentication error
[当我尝试单击“授权”按钮时,似乎未填写Swagger中的变量,并且我一直在尝试SwaggerUI中的不同设置,但这种情况一直在发生。
我们已经这样配置它:
var authority = "https://login.microsoftonline.com/your-aad-tenant-id";
o.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
Scopes = new Dictionary<string, string>
{
["Stuff.Read"] = "Read stuff" // TODO: Replace with your scopes
},
AuthorizationUrl = new Uri(authority + "/oauth2/authorize")
}
}
});