不使用Swagger Azure AD OAuth2令牌

时间:2018-06-08 14:41:33

标签: c# swagger azure-active-directory swagger-ui swashbuckle

我试图使用AzureAD来获取swagger中的令牌并使用它来测试我的控制器。

我在这里使用swagger-ui进行日志记录:

enter image description here

在此之后没有使用我的个人AzureAD ID,我已经登录了。

但是当我尝试使用我的控制器时,我遇到错误:Unauthorized

我的控制器都继承自baseController。所有这些都有[授权]注释。

我真的不知道问题出在哪里。 我使用的是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();
}

1 个答案:

答案 0 :(得分:1)

您已配置Swagger UI以获取具有资源的访问令牌:

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?):< / p>

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端点,因此您选择的范围无关紧要。 令牌将始终包含已经同意的范围,仅此而已。