swagger 身份验证如何工作?
How swagger authentication works?
您好,我已经为我的 .net 核心 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 属性究竟是如何工作的呢?有人可以帮助我理解这些事情吗?任何帮助,将不胜感激。谢谢
关于如何配置Swagger对Azure AD进行身份验证,请参考以下步骤
为您的网站配置 Azure AD API。详情请参考document
一个。创建 Azure AD web api 应用程序
b。 Expose API
c。配置代码
- 配置文件
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"ClientId": "[Client_id-of-web-api-eg-2ec40e65-ba09-4853-bcde-bcb60029e596]",
"TenantId": "<your tenant id>"
},
- 在Stratup.cs
中添加如下代码
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
配置招摇。详情请参考blog。
一个。创建 Azure Web 应用程序
b。配置 API 权限。关于如何配置,可以参考document
c。代码
- 安装SDK
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
- 将以下代码添加到 ConfigureServices 方法中的 Startup.cs:
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" } }
});
});
- 将以下代码添加到 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");
});
您好,我已经为我的 .net 核心 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 属性究竟是如何工作的呢?有人可以帮助我理解这些事情吗?任何帮助,将不胜感激。谢谢
关于如何配置Swagger对Azure AD进行身份验证,请参考以下步骤
为您的网站配置 Azure AD API。详情请参考document
一个。创建 Azure AD web api 应用程序
b。 Expose API
c。配置代码
- 配置文件
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"ClientId": "[Client_id-of-web-api-eg-2ec40e65-ba09-4853-bcde-bcb60029e596]",
"TenantId": "<your tenant id>"
},
- 在Stratup.cs 中添加如下代码
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
配置招摇。详情请参考blog。
一个。创建 Azure Web 应用程序
b。配置 API 权限。关于如何配置,可以参考document
c。代码
- 安装SDK
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
- 将以下代码添加到 ConfigureServices 方法中的 Startup.cs:
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" } } }); });
- 将以下代码添加到 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"); });