tnblog
首页
视频
资源
登录

.net core 3.1 Identity Server4 (NSwag Api授权)

5813人阅读 2021/1/26 17:52 总访问:3657644 评论:0 收藏:0 手机
分类: Ids4

.net core 3.1 Identity Server4 (NSwag Api授权)

创建ApiDemo3接口项目

引入NSwag库

  1. Install-Package NSwag.AspNetCore -Version 13.10.1

修改接口项目

先修改launchSettings.json的地址

  1. {
  2. "profiles": {
  3. "ApIDemo2": {
  4. "commandName": "Project",
  5. "launchBrowser": true,
  6. "launchUrl": "weatherforecast",
  7. "applicationUrl": "http://localhost:9001",
  8. "environmentVariables": {
  9. "ASPNETCORE_ENVIRONMENT": "Development"
  10. }
  11. }
  12. }
  13. }

再在Identity Server4授权服务器的客户端中添加该地址

  1. new Client
  2. {
  3. ClientId = "apidemo2_swagger",
  4. ClientName = "Swagger UI for ApIDemo2",
  5. ClientSecrets = {new Secret("secret".Sha256())},
  6. AllowedGrantTypes = GrantTypes.Code,
  7. // 启动Pkce
  8. RequirePkce = true,
  9. RequireClientSecret = false,
  10. RedirectUris = {
  11. "http://localhost:9001/swagger/oauth2-redirect.html",
  12. "http://localhost:9200/swagger/oauth2-redirect.html",
  13. },
  14. AllowedCorsOrigins = {
  15. "http://localhost:9001",
  16. "http://localhost:9200"
  17. },
  18. AllowedScopes = { "ApiTwo" }
  19. },

回到我们API,添加引用,并在ConfigureServices添加相关代码。

  1. Install-Package IdentityServer4.AccessTokenValidation -Version 3.0.1
  1. services.AddAuthentication("Bearer")
  2. .AddJwtBearer("Bearer", options =>
  3. {
  4. options.Authority = "https://localhost:7200"; // 授权服务器地址
  5. //确定自己是哪个资源(资源名称)
  6. options.Audience = "ApiTwo";
  7. options.RequireHttpsMetadata = false; // 是否使用https进行通信
  8. //取消验证用户以及验证角色
  9. options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
  10. {
  11. ValidateIssuer = false,
  12. ValidateAudience = false
  13. };
  14. });

现在,您可以通过将注册添加到ConfigureServices方法中来将Swagger文档生成添加到项目中。

  1. services.AddOpenApiDocument(options =>
  2. {
  3. options.DocumentName = "v1";
  4. options.Title = "Protected API";
  5. options.Version = "v1";
  6. // 接着我们在下面进行添加代码
  7. });

为了让NSwag了解哪些端点需要访问令牌并将安全范围添加到Swagger文档中,可以使用AspNetCoreOperationSecurityScopeProcessor该类自动扫描您的所有控制器和动作AuthorizationAttributes

  1. options.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
  2. {
  3. Type = OpenApiSecuritySchemeType.OAuth2,
  4. Description = "My Authentication",
  5. Flow = OpenApiOAuth2Flow.AccessCode,
  6. Flows = new OpenApiOAuthFlows()
  7. {
  8. AuthorizationCode = new OpenApiOAuthFlow
  9. {
  10. AuthorizationUrl = "https://localhost:7200/connect/authorize",
  11. TokenUrl = "https://localhost:7200/connect/token",
  12. Scopes = new Dictionary<string, string>
  13. {
  14. {"ApiTwo", "Swagger UI for ApIDemo2"}
  15. }
  16. },
  17. }
  18. });
  19. options.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));

然后,您可以通过在Configure方法中添加以下内容来在管道中启用Swagger文档和UI

  1. app.UseAuthentication();
  2. app.UseAuthorization();
  3. app.UseOpenApi();
  4. app.UseSwaggerUi3(options =>
  5. {
  6. options.OAuth2Client = new OAuth2ClientSettings();
  7. options.OAuth2Client.ClientId = "apidemo2_swagger";
  8. options.OAuth2Client.ClientSecret = "secret";
  9. options.OAuth2Client.AppName = "Demo API - Swagger";
  10. options.OAuth2Client.UsePkceWithAuthorizationCodeGrant = true;
  11. });

访问测试


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

Sqlerver添加用户与授权

添加用户安全性--&gt;登录名,然后右键新建登录名就可以了然后填写好相关信息就可以了右键属性,用户映射可以选择该用户可...

ASP.net core 认证与授权 OAuth 与 OpenID Connect

OAuth 2.0在介绍OAuth之前,我们先简单介绍一下OpenID。OpenID 是一个以用户为中心的数字身份识别框架,它具有开放、分散性...

identity server4 的授权模式

授权模式OAuth2.0 定义了四种授权模式:Implicit:简化模式;直接通过浏览器的链接跳转申请令牌。Client Credentials:客户...

identity server4 四种授权模式

爱情哪有那么复杂,能让你开开心心笑得最甜的那个人就是对的人下面介绍4种模式安全性从低到高客户端模式客户端模式只对客户...

IdentityServer4实现OAuth2.0四种模式之授权码模式

授权码模式隐藏码模式最大不同是授权码模式不直接返回token,而是先返回一个授权码,然后再根据这个授权码去请求token。这...

asp.net权限过滤器授权过滤器

asp.net权限过滤器其实和普通的过滤器差不多,只是它可以配合特性:AllowAnonymous来实现对这个过滤器的忽略。 具体做法就...

.net core 3.1 Identity Server4 (Swagger UI授权)

.net core 3.1 Identity Server4 (Swagger UI授权)[TOC] Identity Server 4的目录:https://www.tnblog.net/hb/article/...

net core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

.net mvc分部页,.net core分部页

.net分部页的三种方式第一种:@Html.Partial(&quot;_分部页&quot;)第二种:@{ Html.RenderPartial(&quot;分部页&quot;);}...

StackExchange.Redis操作redis(net core支持)

官方git开源地址https://github.com/StackExchange/StackExchange.Redis官方文档在docs里边都是官方的文档通过nuget命令下...

.net core 使用session

tip:net core 2.2后可以直接启用session了,不用在自己添加一次session依赖,本身就添加了使用nuget添加引用Microsoft.AspN...

通俗易懂,什么是.net?什么是.net Framework?什么是.net core?

朋友圈@蓝羽 看到一篇文章写的太详细太通俗了,搬过来细细看完,保证你对.NET有个新的认识理解原文地址:https://www.cnblo...

asp.net core2.0 依赖注入 AddTransient与AddScoped的区别

asp.net core主要提供了三种依赖注入的方式其中AddTransient与AddSingleton比较好区别AddTransient瞬时模式:每次都获取一...

.net core 使用 Kestrel

Kestrel介绍 Kestrel是一个基于libuv的跨平台web服务器 在.net core项目中就可以不一定要发布在iis下面了Kestrel体验可以使...

net core中使用cookie

net core中可以使用传统的cookie也可以使用加密的cookieNET CORE中使用传统cookie设置:HttpContext.Response.Cookies.Appe...
这一世以无限游戏为使命!
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术