日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/4 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一日一技:在Ocelot网关中实现IdentityServer4密码模式(password) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述

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<string> aas = new List<string>();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<IdentityResource>{new IdentityResources.OpenId(), //必須要添加,否則報無效的 scope 錯誤new IdentityResources.Profile()};var p = Configuration.GetSection("SSOConfig");services.AddIdentityServer().AddDeveloperSigningCredential().AddInMemoryIdentityResources(idResources).AddInMemoryApiResources(SSOConfig.GetApiResources(p)).AddInMemoryClients(SSOConfig.GetClients(p)).AddResourceOwnerValidator<ResourceOwnerPasswordValidator>().AddProfileService<ProfileService>();services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest);

5、在認證項目進行驗證,測試成功

6、修改地址,在網關項目進行認證,測試成功

代碼地址:

https://gitee.com/conanOpenSource_admin/Example

總結

以上是生活随笔為你收集整理的一日一技:在Ocelot网关中实现IdentityServer4密码模式(password)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。