日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
一日一技:在Ocelot網(wǎng)關(guān)中實(shí)現(xiàn)IdentityServer4密碼模式

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

創(chuàng)新互聯(lián)是專業(yè)的嵐縣網(wǎng)站建設(shè)公司,嵐縣接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行嵐縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

概述

IdentityServer4 是為ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 認(rèn)證框架。將identityserver部署在你的應(yīng)用中,具備如下的特點(diǎn)可以為你的應(yīng)用(如網(wǎng)站、本地應(yī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)用或者移動應(yīng)用等。

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

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

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

下面我們來看下怎么實(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)行賬號密碼認(rèn)證

 
 
 
  1. public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator 
  2.     { 
  3.         public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) 
  4.         { 
  5.             //根據(jù)context.UserName和context.Password與數(shù)據(jù)庫的數(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 aas = new List(); 
  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. //注冊服務(wù) 
  2.             var idResources = new List 
  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() 
  16.             .AddProfileService(); 
  17.             services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest); 

5、在認(rèn)證項(xiàng)目進(jìn)行驗(yàn)證,測試成功

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

代碼地址:

https://gitee.com/conanOpenSource_admin/Example


當(dāng)前名稱:一日一技:在Ocelot網(wǎng)關(guān)中實(shí)現(xiàn)IdentityServer4密碼模式
轉(zhuǎn)載來于:http://www.5511xx.com/article/cdhpoos.html