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

一日一技:在Ocelot網關中實現IdentityServer4密碼模式

安全 應用安全
IdentityServer4 是為ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 認證框架。

 [[398933]]

本文轉載自微信公眾號「UP技術控」,作者conan5566。轉載本文請聯系UP技術控公眾號。

概述

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-09-14 10:48:33

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 延遲調用

2022-06-28 09:31:44

LinuxmacOS系統

2020-05-19 13:55:38

Python加密密碼

2024-11-11 00:38:13

Mypy靜態類型

2021-10-15 21:08:31

PandasExcel對象

2021-04-27 22:15:02

Selenium瀏覽器爬蟲

2024-10-16 21:47:15

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

2023-10-28 12:14:35

爬蟲JavaScriptObject

2024-11-13 09:18:09

2021-04-05 14:47:55

Python多線程事件監控

2022-03-12 20:38:14

網頁Python測試
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 色婷婷av久久久久久久 | 久久久久久国产精品 | 日本三级精品 | 国产成人精品一区二 | 日韩成人在线播放 | 亚洲国产在 | 国产乱码一二三区精品 | 色爱av| 日韩一区二区黄色片 | 久久国产精99精产国高潮 | 亚洲欧洲精品一区 | 999久久久久久久久6666 | 欧美日韩在线一区二区 | 91九色porny首页最多播放 | 国产高清在线视频 | 久久精品国产清自在天天线 | 人人玩人人添人人澡欧美 | 免费黄色成人 | 中文字幕1区| 亚洲视频一区在线观看 | 日韩在线不卡 | 中文成人无字幕乱码精品 | 紧缚调教一区二区三区视频 | 久久国产精彩视频 | 国产免费看 | 无码日韩精品一区二区免费 | 亚洲综合天堂网 | www亚洲一区| 91视频在线观看 | 2021狠狠干| 日本特黄a级高清免费大片 特黄色一级毛片 | www.日韩系列 | 成人高清在线视频 | 性天堂网| avhd101在线成人播放 | 成年人在线观看视频 | h视频免费在线观看 | 男女羞羞免费网站 | 四虎永久免费在线 | 欧美 日韩 国产 成人 | 日韩欧美在线一区 |