繁体   English   中英

未使用Swagger Azure AD OAuth2令牌

[英]Swagger Azure AD OAuth2 token isn't used

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

在这里,我用swagger-ui记录:

在此输入图像描述

在此之后没有使用我的个人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();
}

您已配置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?):

options.TokenValidationParameters = new TokenValidationParameters
{
    ValidAudiences = new List<string>{ "client-id", "app-id-uri"}
};

上面应该放在你的AddJwtBearer配置回调中,你可以删除options.Audience = line。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM