我如何设置Swashbuckle和Swagger UI以使用Open ID Connect Discovery进行授权,以便它可以提供正确的承载令牌?
如何设置Swashbuckle和Swagger UI以授权使用Open ID Connect Discovery(在我的情况下为Azure AD)?
到目前/a>:
SwaggerGenOptions c => {
OpenApiInfo apiInfo = new() { /* ...snip... */ };
c.SwaggerDoc("v1", apiInfo);
IncludeXmlFileForSwagger(c);
// Defines the Open ID Connect discovery scheme - see also https://stackoverflow.com/a/66147996/7453
OpenApiSecurityScheme mainScheme = new()
{
Type = SecuritySchemeType.OpenIdConnect,
OpenIdConnectUrl = new Uri($"https://login.microsoftonline.com/{myTenantId}/.well-known/openid-configuration"),
};
c.AddSecurityDefinition("OpenIdConnect", mainScheme);
// Adds a reference to the above scheme as required for every API action (we can get more nuanced later)
// Note: if just try to use mainScheme instead, it doesn't apply a Bearer token)
OpenApiSecurityScheme securityScheme = new() {
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme, Id = "OpenIdConnect"
}
};
OpenApiSecurityRequirement securityRequirements = new() { {securityScheme, Array.Empty<string>()} };
c.AddSecurityRequirement(securityRequirements);
}
...在静态配置方法中,我有...
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyService v1");
c.OAuthClientId(myClientId);
c.OAuthUsePkce();
});
因此,上述代码使我可以进行身份验证并检查一个称为“ openID”的范围,并显示“锁定的“ padock”。通过Swagger UI,它提供了一个持久的代币,但
我需要以某种方式指定一个名为“ openID”的明确指定
?有效吗?
How do I set up Swashbuckle and Swagger UI to Authorize using Open ID Connect Discovery (in my case, to Azure AD)?
Here's my (not-fully-working) SwaggerGen setup so far, based on https://stackoverflow.com/a/66147996/7453 :
SwaggerGenOptions c => {
OpenApiInfo apiInfo = new() { /* ...snip... */ };
c.SwaggerDoc("v1", apiInfo);
IncludeXmlFileForSwagger(c);
// Defines the Open ID Connect discovery scheme - see also https://stackoverflow.com/a/66147996/7453
OpenApiSecurityScheme mainScheme = new()
{
Type = SecuritySchemeType.OpenIdConnect,
OpenIdConnectUrl = new Uri(quot;https://login.microsoftonline.com/{myTenantId}/.well-known/openid-configuration"),
};
c.AddSecurityDefinition("OpenIdConnect", mainScheme);
// Adds a reference to the above scheme as required for every API action (we can get more nuanced later)
// Note: if just try to use mainScheme instead, it doesn't apply a Bearer token)
OpenApiSecurityScheme securityScheme = new() {
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme, Id = "OpenIdConnect"
}
};
OpenApiSecurityRequirement securityRequirements = new() { {securityScheme, Array.Empty<string>()} };
c.AddSecurityRequirement(securityRequirements);
}
...and in the static configure method, I have...
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyService v1");
c.OAuthClientId(myClientId);
c.OAuthUsePkce();
});
So the above code lets me authenticate and check a scope called "openid" and shows the "locked" padlock". When I try to run a action via the Swagger UI, it supplies a Bearer token but it comes back invalid.
Do I need to somehow specify an explicit named scope, rather than "openid"? How?
Is my setup even right? If so, why isn't the token valid? Any ideas for troubleshooting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于Swagger UI正在使用Web浏览器上下文来提出请求,因此我发现仅在顶部提供一个将它们带到需要AUTH的任何API调用的链接更容易,更简单需要授权的功能。
如果您的API自动重定向并使用浏览器功能登录,这将起作用。用户登录后,Swagger UI的所有将来的HTTP请求都会像直接在浏览器中击中端点一样发送auth cookie。
首先,启动conform.cs中的Swagger配置,包括一个链接以在用户友好的情况下进行签名:
在上面的示例中,我的端点
/api/v1/security/security/security/security/secustin
需要授权;您可以使用需要授权的任何端点。然后,这是您需要适当地显示开放式锁定图标的MethodNeedSauthorizationFilter:
这有效地规避了Swagger UI的内置授权支持,我无法正常工作。
Since Swagger UI is using the web browser context to make the requests, I found it easier and way simpler to just provide a link at the top that will bring them to any API call that requires auth, and then also add a security requirement on all functions that require authorization.
This will work if your API automatically redirects and uses browser functionality to sign in, as I think most do. After the user signs in, all future HTTP requests from the Swagger UI will send the auth cookie just as it would for hitting the endpoints in the browser directly.
First, the Swagger config in Startup.cs, including a link to make signing in user-friendly:
In the example above, my endpoint
/api/v1/security/signIn
requires authorization; you can use any endpoint of yours that requires authorization.Then here's the MethodNeedsAuthorizationFilter you need to enable showing the open padlock icon appropriately:
This effectively circumvents Swagger UI's built-in authorization support, which I could not get working.
SwashBuckle充当OIDC服务器的客户端,以获取访问令牌。因此,首先,您应该添加重定向URI以允许列表
&lt; origin&gt;/swagger/oauth2-redirect.html
(example,https:// https:// localhost:&ltocalhost:&lt; -redirect.html
用于本地开发。Swashbuckle将创建并托管(不是直接)请求以获取发现文档的内容。这样您就应该确保CORS政策是否允许Swashbuckle Origin。
Swashbuckle acts as a client of OIDC server to get access token. Hence, first you should add Redirect Uri to allow list
<origin>/swagger/oauth2-redirect.html
(example,https://localhost:<port>/swagger/oauth2-redirect.html
for local development.Swashbuckle will create and CORS (not direct) request to get content of discovery documents. So that you should ensure if CORS policy allows Swashbuckle origin.