网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在Ocelot网关中实现IdentityServer4密码模式

本篇内容介绍了“如何在Ocelot网关中实现IdentityServer4密码模式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

本篇内容介绍了“如何在Ocelot网关中实现IdentityServer4密码模式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

概述

成都创新互联公司主营平潭网站建设的网络公司,主营网站建设方案,app开发定制,平潭h5微信小程序开发搭建,平潭网站营销推广欢迎平潭等地区企业咨询

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

public class ProfileService : IProfileService     {         public async Task GetProfileDataAsync(ProfileDataRequestContext context)         {             var claims = context.Subject.Claims.ToList();             context.IssuedClaims = claims.ToList();         }         public async Task IsActiveAsync(IsActiveContext context)         {             context.IsActive = true;         }     }

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

public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator     {         public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)         {             //根据context.UserName和context.Password与数据库的数据做校验,判断是否合法             if (context.UserName == "conan" && context.Password == "123")             {                 context.Result = new GrantValidationResult(                 subject: context.UserName,                 authenticationMethod: "custom",                 claims: new Claim[] { new Claim("Name", context.UserName), new Claim("UserId", "111"), new Claim("RealName", "conan"), new Claim("Email", "373197550@qq.com") });             }             else             {                 //验证失败                 context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential");             }         }     }

3、调整AllowedGrantTypes 和AllowedScopes

client.AllowedGrantTypes = GrantTypes.ResourceOwnerPassword;                     List aas = new List();                     aas.AddRange(config.AllowedScopes);                     aas.Add(IdentityServerConstants.StandardScopes.OpenId);                     aas.Add(IdentityServerConstants.StandardScopes.Profile);                     client.AllowedScopes = aas.ToArray();

4、ConfigureServices增加AddInMemoryIdentityResources、AddResourceOwnerValidator、AddProfileService

//注册服务             var idResources = new List             {               new IdentityResources.OpenId(), //必须要添加,否则报无效的 scope 错误               new IdentityResources.Profile()             };               var section = Configuration.GetSection("SSOConfig");             services.AddIdentityServer()          .AddDeveloperSigningCredential()            .AddInMemoryIdentityResources(idResources)          .AddInMemoryApiResources(SSOConfig.GetApiResources(section))          .AddInMemoryClients(SSOConfig.GetClients(section))               .AddResourceOwnerValidator()             .AddProfileService();             services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest);

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

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

代码地址:

https://gitee.com/conanOpenSource_admin/Example


本文题目:如何在Ocelot网关中实现IdentityServer4密码模式
转载注明:http://bjjierui.cn/article/dpipi.html

其他资讯