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

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

開(kāi)發(fā) 后端
IdentityServer4 是為ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 認(rèn)證框架。

[[423644]]

概述

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)。在各種類(lèi)型的應(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-05-12 00:12:37

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

2021-03-16 06:55:49

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

2021-09-13 20:38:47

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

2021-03-12 21:19:15

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)類(lèi)型

2021-10-15 21:08:31

PandasExcel對(duì)象

2021-04-27 22:15:02

Selenium瀏覽器爬蟲(chóng)

2024-10-16 21:47:15

2025-05-28 03:15:00

Scrapy數(shù)據(jù)sleep

2021-04-12 21:19:01

PythonMakefile項(xiàng)目

2021-06-08 21:36:24

PyCharm爬蟲(chóng)Scrapy

2021-01-22 05:47:21

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

2021-07-26 21:15:10

LRU緩存MongoDB

2023-10-28 12:14:35

爬蟲(chóng)JavaScriptObject

2024-11-13 09:18:09

2024-07-30 08:16:18

Python代碼工具

2021-04-05 14:47:55

Python多線程事件監(jiān)控
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 日本aa毛片a级毛片免费观看 | 久久久久无码国产精品一区 | 九九导航| 99久久精品免费看国产四区 | 男女羞羞视频在线观看 | 中文一区二区 | 国产免费一区 | 91精品国产色综合久久不卡98口 | 一级一片在线观看 | 久久久久免费精品国产 | 91大神在线资源观看无广告 | 免费一区 | 国产一区不卡 | 国产乱码久久久 | 久久躁日日躁aaaaxxxx | 大乳boobs巨大吃奶挤奶 | 日韩一区二区在线看 | 久久三级影院 | 日韩在线视频免费观看 | 水蜜桃久久夜色精品一区 | 一区二区三区四区在线 | 香蕉婷婷| 一级片av | 黄色电影在线免费观看 | 欧美日韩a| 国产亚洲精品美女久久久久久久久久 | 欧美日韩一区二区三区视频 | 久久久精品视频免费 | 国产精品视频一区二区三区不卡 | 涩涩视频网站在线观看 | 91免费版在线观看 | 国产精品久久久久久久久久久久 | 一区二区av | 午夜a级理论片915影院 | 亚洲色欲色欲www | 免费观看色 | 国产专区在线 | 亚洲有码转帖 | 国产精品99久久久久久www | 日日操操 | 日本视频中文字幕 |