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

2025-05-29 0 77

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

概述

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. publicclassProfileService:IProfileService
  2. {
  3. publicasyncTaskGetProfileDataAsync(ProfileDataRequestContextcontext)
  4. {
  5. varclaims=context.Subject.Claims.ToList();
  6. context.IssuedClaims=claims.ToList();
  7. }
  8. publicasyncTaskIsActiveAsync(IsActiveContextcontext)
  9. {
  10. context.IsActive=true;
  11. }
  12. }

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

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

3、调整AllowedGrantTypes 和AllowedScopes

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

4、ConfigureServices增加AddInMemoryIdentityResources、AddResourceOwnerValidator、AddProfileService

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

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

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

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

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

代码地址:

https://gitee.com/conanOpenSource_admin/Example

原文链接:https://mp.weixin.qq.com/s/7EpcIukJmG7CjfKw5nnPrQ

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 在Ocelot网关中实现IdentityServer4密码模式 https://www.kuaiidc.com/97728.html

相关文章

发表评论
暂无评论