.NET Core Web Api Azure AD和Swagger不进行身份验证 [英] .NET Core Web Api Azure AD and Swagger not authenticating

查看:67
本文介绍了.NET Core Web Api Azure AD和Swagger不进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Azure AD身份验证和Swagger设置.NET Core Web API,但是在尝试进行身份验证时会出现错误.

I am trying to set up a .NET Core Web API with Azure AD authentication and Swagger but it gives an error when trying to authenticate.

我正在遵循此指南:http://www.sharepointconfig.com/2018/08/configure-swagger-to-authenticate-against-azure-ad/

ConfigureServices包含以下代码:

ConfigureServices contains this code:

        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"] } });
            });

我一直遇到的问题是这个:招摇认证错误

The problem I keep running in to is this one: Swagger authentication error

当我尝试单击授权"按钮时,似乎未填写Swagger中的变量,并且我一直在尝试SwaggerUI中的其他设置,但这种情况一直在发生.

It looks like when I try to click the Authorize button a variable in Swagger is not filled in and I have been trying different settings in the SwaggerUI but this keeps happening.

推荐答案

我们已将其配置如下:

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")
        }
    }
});

这篇关于.NET Core Web Api Azure AD和Swagger不进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆