- 資訊首頁(yè) > 網(wǎng)絡(luò )安全 >
- 如何在Ocelot網(wǎng)關(guān)中實(shí)現IdentityServer4密碼模式
本篇內容介紹了“如何在Ocelot網(wǎng)關(guān)中實(shí)現IdentityServer4密碼模式”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!
IdentityServer4 是為ASP.NET Core 2.系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 認證框架。將identityserver部署在你的應用中,具備如下的特點(diǎn)可以為你的應用(如網(wǎng)站、本地應用、移動(dòng)端、服務(wù))做集中式的登錄邏輯和工作流控制。IdentityServer是完全實(shí)現了OpenID Connect協(xié)議標準。在各種類(lèi)型的應用上實(shí)現單點(diǎn)登錄登出。為各種各樣的客戶(hù)端頒發(fā)access token令牌,如服務(wù)與服務(wù)之間的通訊、網(wǎng)站應用、SPAS和本地應用或者移動(dòng)應用等。
OAuth 2.0 默認四種授權模式(GrantType):
授權碼模式(authorization_code)
簡(jiǎn)化模式(implicit)
密碼模式(password)
客戶(hù)端模式(client_credentials)
我們一般項目在api訪(fǎng)問(wèn)的時(shí)候,大部分是基于賬號密碼的方式進(jìn)行訪(fǎng)問(wèn)接口。比如app端的用戶(hù)。
下面我們來(lái)看下怎么實(shí)現密碼模式(password)。
1、在認證項目中,創(chuàng )建ProfileService
public class ProfileService : IProfileService { public async Task GetProfileDataAsync(ProfileDataRequestContext context) { var claims = context.Subject.Claims.ToList(); context.IssuedClaims = claims.ToList(); } public async Task IsActiveAsync(IsActiveContext context) { context.IsActive = true; } }
2、創(chuàng )建ResourceOwnerPasswordValidator,進(jìn)行賬號密碼認證
public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator { public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) { //根據context.UserName和context.Password與數據庫的數據做校驗,判斷是否合法 if (context.UserName == "conan" && context.Password == "123") { context.Result = new GrantValidationResult( subject: context.UserName, authenticationMethod: "custom", claims: new Claim[] { new Claim("Name", context.UserName), new Claim("UserId", "111"), new Claim("RealName", "conan"), new Claim("Email", "373197550@qq.com") }); } else { //驗證失敗 context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential"); } } }
3、調整AllowedGrantTypes 和AllowedScopes
client.AllowedGrantTypes = GrantTypes.ResourceOwnerPassword; List<string> aas = new List<string>(); aas.AddRange(config.AllowedScopes); aas.Add(IdentityServerConstants.StandardScopes.OpenId); aas.Add(IdentityServerConstants.StandardScopes.Profile); client.AllowedScopes = aas.ToArray();
4、ConfigureServices增加AddInMemoryIdentityResources、AddResourceOwnerValidator、AddProfileService
//注冊服務(wù) var idResources = new List<IdentityResource> { new IdentityResources.OpenId(), //必須要添加,否則報無(wú)效的 scope 錯誤 new IdentityResources.Profile() }; var section = Configuration.GetSection("SSOConfig"); services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryIdentityResources(idResources) .AddInMemoryApiResources(SSOConfig.GetApiResources(section)) .AddInMemoryClients(SSOConfig.GetClients(section)) .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>() .AddProfileService<ProfileService>(); services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Latest);
5、在認證項目進(jìn)行驗證,測試成功
6、修改地址,在網(wǎng)關(guān)項目進(jìn)行認證,測試成功
代碼地址:
https://gitee.com/conanOpenSource_admin/Example
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站