自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一日一技:在Ocelot網(wǎng)關(guān)中實(shí)現(xiàn)IdentityServer4密碼模式

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

 [[398933]]

本文轉(zhuǎn)載自微信公眾號(hào)「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請(qǐng)聯(lián)系UP技術(shù)控公眾號(hào)。

概述

IdentityServer4 是為ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 認(rèn)證框架。將identityserver部署在你的應(yīng)用中,具備如下的特點(diǎn)可以為你的應(yīng)用(如網(wǎng)站、本地應(yīng)用、移動(dòng)端、服務(wù))做集中式的登錄邏輯和工作流控制。IdentityServer是完全實(shí)現(xiàn)了OpenID Connect協(xié)議標(biāo)準(zhǔn)。在各種類型的應(yīng)用上實(shí)現(xiàn)單點(diǎn)登錄登出。為各種各樣的客戶端頒發(fā)access token令牌,如服務(wù)與服務(wù)之間的通訊、網(wǎng)站應(yīng)用、SPAS和本地應(yīng)用或者移動(dòng)應(yīng)用等。

OAuth 2.0 默認(rèn)四種授權(quán)模式(GrantType):

  • 授權(quán)碼模式(authorization_code)
  • 簡(jiǎn)化模式(implicit)
  • 密碼模式(password)
  • 客戶端模式(client_credentials)

我們一般項(xiàng)目在api訪問(wèn)的時(shí)候,大部分是基于賬號(hào)密碼的方式進(jìn)行訪問(wèn)接口。比如app端的用戶。

下面我們來(lái)看下怎么實(shí)現(xiàn)密碼模式(password)。

主要實(shí)現(xiàn)方式

1、在認(rèn)證項(xiàng)目中,創(chuàng)建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、創(chuàng)建ResourceOwnerPasswordValidator,進(jìn)行賬號(hào)密碼認(rèn)證

  1. public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator 
  2.     { 
  3.         public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) 
  4.         { 
  5.             //根據(jù)context.UserName和context.Password與數(shù)據(jù)庫(kù)的數(shù)據(jù)做校驗(yàn),判斷是否合法 
  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.                 //驗(yàn)證失敗 
  16.                 context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential"); 
  17.             } 
  18.         } 
  19.     } 

3、調(diào)整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. //注冊(cè)服務(wù) 
  2.             var idResources = new List<IdentityResource> 
  3.             { 
  4.               new IdentityResources.OpenId(), //必須要添加,否則報(bào)無(wú)效的 scope 錯(cuò)誤 
  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、在認(rèn)證項(xiàng)目進(jìn)行驗(yàn)證,測(cè)試成功

6、修改地址,在網(wǎng)關(guān)項(xiàng)目進(jìn)行認(rèn)證,測(cè)試成功

代碼地址:

https://gitee.com/conanOpenSource_admin/Example

 

責(zé)任編輯:武曉燕 來(lái)源: UP技術(shù)控
相關(guān)推薦

2021-09-14 10:48:33

Ocelot網(wǎng)關(guān)

2021-03-16 06:55:49

Server4認(rèn)證網(wǎng)關(guān)

2021-03-12 21:19:15

Python鏈?zhǔn)?/a>調(diào)用

2021-09-13 20:38:47

Python鏈?zhǔn)?/a>調(diào)用

2021-07-27 21:32:57

Python 延遲調(diào)用

2022-06-28 09:31:44

LinuxmacOS系統(tǒng)

2020-05-19 13:55:38

Python加密密碼

2024-11-11 00:38:13

Mypy靜態(tài)類型

2021-04-27 22:15:02

Selenium瀏覽器爬蟲(chóng)

2021-10-15 21:08:31

PandasExcel對(duì)象

2024-10-16 21:47:15

2021-04-12 21:19:01

PythonMakefile項(xiàng)目

2021-07-26 21:15:10

LRU緩存MongoDB

2021-01-22 05:47:21

Python關(guān)鍵字函數(shù)

2023-10-28 12:14:35

爬蟲(chóng)JavaScriptObject

2022-03-12 20:38:14

網(wǎng)頁(yè)Python測(cè)試

2021-04-19 23:29:44

MakefilemacOSLinux

2024-07-30 08:11:16

2021-04-05 14:47:55

Python多線程事件監(jiān)控

2024-11-13 09:18:09

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)