
.net core 3.1 Identity Server4 (NSwag Api授权)
创建ApiDemo3接口项目
引入NSwag库
Install-Package NSwag.AspNetCore -Version 13.10.1
修改接口项目
先修改launchSettings.json
的地址
{
"profiles": {
"ApIDemo2": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:9001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
再在Identity Server4授权服务器的客户端中添加该地址
new Client
{
ClientId = "apidemo2_swagger",
ClientName = "Swagger UI for ApIDemo2",
ClientSecrets = {new Secret("secret".Sha256())},
AllowedGrantTypes = GrantTypes.Code,
// 启动Pkce
RequirePkce = true,
RequireClientSecret = false,
RedirectUris = {
"http://localhost:9001/swagger/oauth2-redirect.html",
"http://localhost:9200/swagger/oauth2-redirect.html",
},
AllowedCorsOrigins = {
"http://localhost:9001",
"http://localhost:9200"
},
AllowedScopes = { "ApiTwo" }
},
回到我们API,添加引用,并在ConfigureServices
添加相关代码。
Install-Package IdentityServer4.AccessTokenValidation -Version 3.0.1
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "https://localhost:7200"; // 授权服务器地址
//确定自己是哪个资源(资源名称)
options.Audience = "ApiTwo";
options.RequireHttpsMetadata = false; // 是否使用https进行通信
//取消验证用户以及验证角色
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateIssuer = false,
ValidateAudience = false
};
});
现在,您可以通过将注册添加到ConfigureServices
方法中来将Swagger
文档生成添加到项目中。
services.AddOpenApiDocument(options =>
{
options.DocumentName = "v1";
options.Title = "Protected API";
options.Version = "v1";
// 接着我们在下面进行添加代码
});
为了让NSwag了解哪些端点需要访问令牌并将安全范围添加到Swagger文档中,可以使用AspNetCoreOperationSecurityScopeProcessor
该类自动扫描您的所有控制器和动作AuthorizationAttributes
。
options.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.OAuth2,
Description = "My Authentication",
Flow = OpenApiOAuth2Flow.AccessCode,
Flows = new OpenApiOAuthFlows()
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = "https://localhost:7200/connect/authorize",
TokenUrl = "https://localhost:7200/connect/token",
Scopes = new Dictionary<string, string>
{
{"ApiTwo", "Swagger UI for ApIDemo2"}
}
},
}
});
options.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
然后,您可以通过在Configure
方法中添加以下内容来在管道中启用Swagger
文档和UI
:
app.UseAuthentication();
app.UseAuthorization();
app.UseOpenApi();
app.UseSwaggerUi3(options =>
{
options.OAuth2Client = new OAuth2ClientSettings();
options.OAuth2Client.ClientId = "apidemo2_swagger";
options.OAuth2Client.ClientSecret = "secret";
options.OAuth2Client.AppName = "Demo API - Swagger";
options.OAuth2Client.UsePkceWithAuthorizationCodeGrant = true;
});
访问测试
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术