我正在尝试使用 AzureAD Swagger 获取 token 并用它测试我的 Controller 。
这里我使用 swagger-ui 登录:
在此之后,无需使用我个人的 AzureAD Id,我就已经登录了。
但是当我尝试使用我的 Controller 时,出现错误:Unauthorized
我的 Controller 都继承自一个 baseController。它们都有 [Authorize] 注释。
我真的不知道问题出在哪里。 我正在使用 Swashbuckle.AspNetCore v2.5.0
一些配置代码。
Statup.cs
public virtual void ConfigureServices(IServiceCollection services)
{
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = "{client_id}";
options.Authority = "https://login.microsoftonline.com/{tenant_id}";
});
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v2", new Info
{
Title = "API",
Version = "v2",
Description = "Api Help",
Contact = new Contact()
{
Name = "John Doe",
Email = "john.doe@somewhere.com"
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.AddSecurityDefinition("OAuth2", new OAuth2Scheme()
{
Flow = "implicit",
Type = "oauth2",
TokenUrl = "https://login.microsoftonline.com/{tenant_id}/oauth2/token",
Description = "OAuth2 Implicit Grant",
AuthorizationUrl = "https://login.microsoftonline.com/{tenant_id}/oauth2/authorize",
Scopes = new Dictionary<string, string>()
{
{ "user_impersonation", "Access api" }
}
});
options.OperationFilter<FileOperation>();
options.DescribeAllEnumsAsStrings();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMiddleware<ExceptionHandlerMiddleware>();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v2/swagger.json", "API V2");
c.RoutePrefix = "Help";
c.OAuthClientId("{client_id_swagger_app}"); //Swagger
//c.OAuthClientId("client_id_api_app"); //api
c.DisplayRequestDuration();
c.OAuthClientSecret("{secret_client_key_swagger}"); //swagger secret
c.OAuthAppName("Swagger");
c.OAuthRealm("http://localhost:1234/swagger/ui/o2c-html");
c.OAuthScopeSeparator(" ");
c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
c.OAuthAdditionalQueryStringParams(new Dictionary<string, string>
{
{ "resource", "{app_ID_URI}"} //api
});
});
app.UseAuthentication();
app.UseMvc();
}
最佳答案
您已将 Swagger UI 配置为使用资源获取访问 token :
c.OAuthAdditionalQueryStringParams(new Dictionary<string, string>
{
{ "resource", "{app_ID_URI}"} //api
});
但是您已将有效受众配置为:
options.Audience = "{client_id}";
使用 App ID URI 的 Swagger UI 很好,您可以将 API 配置为同时接受客户端 ID 和 URI 作为受众(Azure AD 允许同时使用两者,那么为什么不也这样配置您的 API 呢?):
options.TokenValidationParameters = new TokenValidationParameters
{
ValidAudiences = new List<string>{ "client-id", "app-id-uri"}
};
上面的内容应该放在你的 AddJwtBearer
配置回调中,你可以删除 options.Audience =
行。
要指定每个操作所需的范围,您需要在 services.AddSwaggerGen(options =>{})
中添加一个操作过滤器:
options.OperationFilter<AuthenticationOperationFilter>();
并定义每个操作都需要 OAuth 范围的过滤器:
public class AuthenticationOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
operation.Security = new List<IDictionary<string, IEnumerable<string>>>
{
new Dictionary<string, IEnumerable<string>>
{
//Note "OAuth2" casing here must match the definition you use in Swagger UI config
["OAuth2"] = new List<string>{ "user_impersonation" }
}
};
}
}
您在 Swagger UI 配置中定义的内容定义了用户在使用 UI 时可以选择的范围。 请记住,由于您使用的是 Azure AD v1 终结点,因此您选择的范围并不重要。 token 将始终包含已同意的范围,仅此而已。
关于c# - 未使用 Swagger Azure AD OAuth2 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50763080/