一日一技:在Ocelot网关中实现IdentityServer4密码模式

开发 后端
IdentityServer4 是为ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 认证框架。

[[423644]]

概述

IdentityServer4 是为ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 认证框架。将identityserver部署在你的应用中,具备如下的特点可以为你的应用(如网站、本地应用、移动端、服务)做集中式的登录逻辑和工作流控制。IdentityServer是完全实现了OpenID Connect协议标准。在各种类型的应用上实现单点登录登出。为各种各样的客户端颁发access token令牌,如服务与服务之间的通讯、网站应用、SPAS和本地应用或者移动应用等。

OAuth 2.0 默认四种授权模式(GrantType):

授权码模式(authorization_code)

简化模式(implicit)

密码模式(password)

客户端模式(client_credentials)

我们一般项目在api访问的时候,大部分是基于账号密码的方式进行访问接口。比如app端的用户。

下面我们来看下怎么实现密码模式(password)。

主要实现方式

1、在认证项目中,创建ProfileService

  1. public class ProfileService : IProfileService 
  2.     { 
  3.         public async Task GetProfileDataAsync(ProfileDataRequestContext context) 
  4.         { 
  5.             var claims = context.Subject.Claims.ToList(); 
  6.             context.IssuedClaims = claims.ToList(); 
  7.         } 
  8.         public async Task IsActiveAsync(IsActiveContext context) 
  9.         { 
  10.             context.IsActive = true
  11.         } 
  12.     } 

2、创建ResourceOwnerPasswordValidator,进行账号密码认证

  1. public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator 
  2.     { 
  3.         public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) 
  4.         { 
  5.             //根据context.UserName和context.Password与数据库的数据做校验,判断是否合法 
  6.             if (context.UserName == "conan" && context.Password == "123"
  7.             { 
  8.                 context.Result = new GrantValidationResult( 
  9.                 subject: context.UserName, 
  10.                 authenticationMethod: "custom"
  11.                 claims: new Claim[] { new Claim("Name", context.UserName), new Claim("UserId""111"), new Claim("RealName""conan"), new Claim("Email""373197550@qq.com") }); 
  12.             } 
  13.             else 
  14.             { 
  15.                 //验证失败 
  16.                 context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential"); 
  17.             } 
  18.         } 
  19.     } 

3、调整AllowedGrantTypes 和AllowedScopes

  1. client.AllowedGrantTypes = GrantTypes.ResourceOwnerPassword; 
  2.                     List<string> aas = new List<string>(); 
  3.                     aas.AddRange(config.AllowedScopes); 
  4.                     aas.Add(IdentityServerConstants.StandardScopes.OpenId); 
  5.                     aas.Add(IdentityServerConstants.StandardScopes.Profile); 
  6.                     client.AllowedScopes = aas.ToArray(); 
  7.                    

4、ConfigureServices增加AddInMemoryIdentityResources、AddResourceOwnerValidator、AddProfileService

  1. //注册服务 
  2.             var idResources = new List<IdentityResource> 
  3.             { 
  4.               new IdentityResources.OpenId(), //必须要添加,否则报无效的 scope 错误 
  5.               new IdentityResources.Profile() 
  6.             }; 
  7.  
  8.  
  9.             var section = Configuration.GetSection("SSOConfig"); 
  10.             services.AddIdentityServer() 
  11.          .AddDeveloperSigningCredential() 
  12.            .AddInMemoryIdentityResources(idResources) 
  13.          .AddInMemoryApiResources(SSOConfig.GetApiResources(section)) 
  14.          .AddInMemoryClients(SSOConfig.GetClients(section)) 
  15.               .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>() 
  16.             .AddProfileService<ProfileService>(); 
  17.             services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest); 

5、在认证项目进行验证,测试成功

6、修改地址,在网关项目进行认证,测试成功

代码地址: 

https://gitee.com/conanOpenSource_admin/Example

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2021-05-12 00:12:37

Ocelot网关密码

2021-03-16 06:55:49

Server4认证网关

2021-03-12 21:19:15

Python链式调用

2021-09-13 20:38:47

Python链式调用

2021-07-27 21:32:57

Python 延迟调用

2022-06-28 09:31:44

LinuxmacOS系统

2020-05-19 13:55:38

Python加密密码

2021-10-15 21:08:31

PandasExcel对象

2021-04-27 22:15:02

Selenium浏览器爬虫

2021-01-22 05:47:21

Python关键字函数

2021-07-26 21:15:10

LRU缓存MongoDB

2021-04-12 21:19:01

PythonMakefile项目

2021-10-06 23:17:26

Python抽象类接口

2021-06-08 21:36:24

PyCharm爬虫Scrapy

2021-04-05 14:47:55

Python多线程事件监控

2023-10-28 12:14:35

爬虫JavaScriptObject

2022-03-12 20:38:14

网页Python测试

2021-04-19 23:29:44

MakefilemacOSLinux

2022-01-03 15:06:30

macOSWi-Fi密码

2021-10-03 20:08:29

HTTP2Scrapy
点赞
收藏

51CTO技术栈公众号