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

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。

创新互联是专业的岚县网站建设公司,岚县接单;提供成都做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行岚县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

概述

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 aas = new List(); 
  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 
  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() 
  16.             .AddProfileService(); 
  17.             services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest); 

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

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

代码地址:

https://gitee.com/conanOpenSource_admin/Example

本文标题:一日一技:在Ocelot网关中实现IdentityServer4密码模式
网址分享:http://www.shufengxianlan.com/qtweb/news43/2743.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联