swagger 身份验证如何工作?

问题描述 投票:0回答:2

您好,我已经为我的 .net core Web 应用程序开发了 swagger UI。我已经添加了身份验证。我已在 Azure AD 中注册了两个应用程序。一种用于 Swagger,一种用于后端 .Net 核心应用程序。下面是我的代码。

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });

                c.AddSecurityDefinition("oauth2", new OAuth2Scheme
                {
                    Type = "oauth2",
                    Flow = "implicit",
                    AuthorizationUrl = swaggerUIOptions.AuthorizationUrl,
                    TokenUrl = swaggerUIOptions.TokenUrl
                });
                c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
                {
                        { "oauth2", new[] { "readAccess", "writeAccess" } }
                });
            });

在上面的代码中我指示了类型和流程。还指定 AuthorizationUrl 和令牌 url。当谈到作用域时,如果我添加作用域,那么这意味着我的 Swagger 可以访问添加的作用域,或者我的后端 api 可以访问这些作用域?然后我有下面的代码。

c.OAuthClientId(swaggerUIOptions.ClientId);
                c.OAuthClientSecret(swaggerUIOptions.ClientSecret);
                c.OAuthRealm(azureActiveDirectoryOptions.ClientId);
                c.OAuthAppName("Swagger");
                c.OAuthAdditionalQueryStringParams(new { resource = azureActiveDirectoryOptions.ClientId });
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

当我们开发 swagger 时,我们正在获取 swagger 应用程序或后端应用程序的访问令牌吗?我还有 c.OAuthRealm 并传递我的后端应用程序客户端 ID。这行代码实际上做了什么?另外,当我在 API 顶部添加 [Authorize] 属性,然后如果我尝试直接点击 api ,它将不起作用。只有经过身份验证后才会起作用。那么 Authorize 属性到底是如何工作的呢?有人可以帮助我理解这些事情吗?任何帮助,将不胜感激。谢谢

authentication .net-core azure-active-directory swagger swashbuckle
2个回答
1
投票

关于如何配置Swagger来针对Azure AD进行身份验证,请参考以下步骤

  • 为您的 Web API 配置 Azure AD。更多详情请参阅文档

    a.创建 Azure AD Web API 应用程序

    b. 公开API

    c.配置代码

    1. 配置文件
"AzureAd": {
 "Instance": "https://login.microsoftonline.com/",
 "ClientId": "[Client_id-of-web-api-eg-2ec40e65-ba09-4853-bcde-bcb60029e596]",

 "TenantId": "<your tenant id>"
},
  1. 在 Stratup.cs 中添加以下代码
 services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
                .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

  • 配置 swagger。欲了解更多详情,请参阅blog

    a.创建 Azure Web 应用程序

    b.配置API权限。关于如何配置,可以参考文档

    c.代码

    1. 安装SDK
    <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
    
    1. 在Startup.cs的ConfigureServices方法中添加以下代码:
    services.AddSwaggerGen(c =>
    {
    c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    c.AddSecurityDefinition("oauth2", new OAuth2Scheme
    {
    Type = "oauth2",
    Flow = "implicit",
    AuthorizationUrl = $"https://login.microsoftonline.com/{Configuration["AzureAD:TenantId"]}/oauth2/authorize",
    Scopes = new Dictionary<string, string>
    {
     { "user_impersonation", "Access API" }
    }
    });
    c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
    {
    { "oauth2", new[] { "user_impersonation" } }
    });
    });
    
    1. 将以下代码添加到Configure方法中:
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
    c.OAuthClientId(Configuration["Swagger:ClientId"]);
    c.OAuthClientSecret(Configuration["Swagger:ClientSecret"]);
    c.OAuthRealm(Configuration["AzureAD:ClientId"]);
    c.OAuthAppName("My API V1");
    c.OAuthScopeSeparator(" ");
    c.OAuthAdditionalQueryStringParams(new { resource = Configuration["AzureAD:ClientId"] });
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });
    
    

0
投票

https:///service/oauth/{OauthAppName}/authorize?client_id={LoginRadius API key}&redirect_uri={回调URL}&scope={Scope}&response_type=token&state={随机长字符串}

© www.soinside.com 2019 - 2024. All rights reserved.