成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一日一技:在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-09-13 20:38:47

Python鏈式調用

2021-03-12 21:19:15

Python鏈式調用

2021-07-27 21:32:57

Python 延遲調用

2020-05-19 13:55:38

Python加密密碼

2022-06-28 09:31:44

LinuxmacOS系統

2024-11-11 00:38:13

Mypy靜態類型

2024-10-16 21:47:15

2021-10-15 21:08:31

PandasExcel對象

2021-04-27 22:15:02

Selenium瀏覽器爬蟲

2025-05-28 03:15:00

Scrapy數據sleep

2021-04-12 21:19:01

PythonMakefile項目

2021-06-08 21:36:24

PyCharm爬蟲Scrapy

2021-01-22 05:47:21

Python關鍵字函數

2021-07-26 21:15:10

LRU緩存MongoDB

2021-10-03 20:08:29

HTTP2Scrapy

2024-05-24 09:07:06

JSONprint字符串

2021-05-08 19:33:51

移除字符零寬

2024-07-19 18:23:17

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品免费av| 欧美 日韩 国产 成人 在线 | 国产精品一区二 | 久久久国产一区二区三区 | 无码一区二区三区视频 | 精品成人av | 国产日韩一区二区三区 | 日韩免费视频一区二区 | 亚洲精品99999 | 91免费看片神器 | 亚洲二区在线 | 成人免费区一区二区三区 | 麻豆av网站 | 美日韩精品 | 日韩电影中文字幕在线观看 | 一区欧美| 日本精品一区二区三区在线观看 | 国产精品久久久久久久久久久新郎 | 久久一区二区三区四区 | 色婷婷av777 av免费网站在线 | 久久久久久久综合色一本 | 亚洲国产aⅴ精品 | 欧美一级黄色免费看 | 国产精品视频网 | 狠狠av | 日韩中文字幕 | 成人欧美一区二区 | 在线免费观看视频黄 | 特级黄一级播放 | 久久国产视频一区 | 色爱综合网| 日本午夜免费福利视频 | 日韩欧美在线一区二区 | 波多野结衣精品在线 | 中文字幕在线观看一区二区 | 国产精品一区在线 | 国产精品视频一二三区 | 国产精品久久一区 | 91精品国产色综合久久 | av一区二区三区在线观看 | 先锋资源网站 |