如何使用 Swashbuckle 在 ASP.NET Core for Swagger UI 中进行 OAuth2 授权
How to do OAuth2 Authorization in ASP.NET Core for Swagger UI using Swashbuckle
DotNet Core 2
中使用客户端凭据流的 Swashbuckle OAuth2 授权
我想设置隐式 Flow
、AuthorizationUrl
、不同 Scopes
、默认选择 Client-id
、
因此,在单击授权后,它应该导航到不同的选项卡,打开 AuthorizationUrl
并让用户登录 Swagger
。因此,下次用户可以看到 log out
选项。
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info()
{
Title = "",
Description = "All rights reserved."
});
c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Flow = "implicit",
AuthorizationUrl = "https://...",
Scopes = new Dictionary<string, string> {
{ "", "Read/Write" }
}
});
});
和Configure()
有,
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "iModelAcquisitionService");
});
您可以尝试以下步骤来启用隐式 Oauth2 流程:
更改 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/cb1c3f2e-a2dd-4fde-bf8f-f75ab18b21ac/oauth2/authorize",
Scopes = new Dictionary<string, string>
{
{ "accessApi", "Access read operations" },
},
TokenUrl = "https://login.microsoftonline.com/cb1c3f2e-a2dd-4fde-bf8f-f75ab18b21ac/oauth2/token"
});
});
并在 Configure 方法中替换以下内容:
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.OAuthClientId("19c73866-562f-482a-bafb-89d9fe9b0aaa");
c.OAuthAppName("Swagger Api Calls");
});
- 转到 swagger 端点:http://localhost:xxx/swagger 并单击
Authorize
按钮。
DotNet Core 2
我想设置隐式 Flow
、AuthorizationUrl
、不同 Scopes
、默认选择 Client-id
、
因此,在单击授权后,它应该导航到不同的选项卡,打开 AuthorizationUrl
并让用户登录 Swagger
。因此,下次用户可以看到 log out
选项。
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info()
{
Title = "",
Description = "All rights reserved."
});
c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Flow = "implicit",
AuthorizationUrl = "https://...",
Scopes = new Dictionary<string, string> {
{ "", "Read/Write" }
}
});
});
和Configure()
有,
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "iModelAcquisitionService");
});
您可以尝试以下步骤来启用隐式 Oauth2 流程:
更改 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/cb1c3f2e-a2dd-4fde-bf8f-f75ab18b21ac/oauth2/authorize", Scopes = new Dictionary<string, string> { { "accessApi", "Access read operations" }, }, TokenUrl = "https://login.microsoftonline.com/cb1c3f2e-a2dd-4fde-bf8f-f75ab18b21ac/oauth2/token" }); });
并在 Configure 方法中替换以下内容:
app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.OAuthClientId("19c73866-562f-482a-bafb-89d9fe9b0aaa"); c.OAuthAppName("Swagger Api Calls"); });
- 转到 swagger 端点:http://localhost:xxx/swagger 并单击
Authorize
按钮。