成人免费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靜態類型

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

2023-10-28 12:14:35

爬蟲JavaScriptObject

2024-11-13 09:18:09

2021-04-05 14:47:55

Python多線程事件監控

2021-04-19 23:29:44

MakefilemacOSLinux
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美午夜视频 | 久久久久网站 | 国产区在线观看 | www.亚洲视频.com | 精品伊人久久 | 中文字幕日韩一区二区 | 乱码av午夜噜噜噜噜动漫 | 亚洲a视频 | 国产午夜精品一区二区三区嫩草 | 毛片一区二区三区 | 亚洲精品v| 国产精品一区二区在线观看 | 欧美日韩成人在线观看 | 亚洲国产成人精品在线 | 久久久久国产精品一区二区 | 成人免费视频网站在线观看 | 国产精品99久久久久久动医院 | 黄色在线观看 | 国产成人高清视频 | 色伊人网| 色综合天天网 | 国产一区二区日韩 | 成人超碰| 国产精品视频一区二区三区不卡 | 中文字幕乱码一区二区三区 | 亚洲精久久久 | 中文在线一区二区 | 欧美一级久久 | 伊人久久在线观看 | 中文字幕91 | 三级黄色网址 | 欧美激情精品久久久久久 | 九九久久精品视频 | 久久99精品久久久久久 | 国产精品一区二区在线免费观看 | 成年人在线观看 | 亚洲在线一区二区 | 天天插天天舔 | 国产一区二区三区四区三区四 | 一区二区三区影院 | 免费在线成人 |