您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何在Ocelot網(wǎng)關(guān)中實(shí)現(xiàn)IdentityServer4密碼模式”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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)。
1、在認(rèn)證項(xiàng)目中,創(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)行賬號(hào)密碼認(rèn)證
public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator { public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) { //根據(jù)context.UserName和context.Password與數(shù)據(jù)庫(kù)的數(shù)據(jù)做校驗(yàn),判斷是否合法 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 { //驗(yàn)證失敗 context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential"); } } }
3、調(diào)整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
//注冊(cè)服務(wù) var idResources = new List<IdentityResource> { new IdentityResources.OpenId(), //必須要添加,否則報(bào)無(wú)效的 scope 錯(cuò)誤 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、在認(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
“如何在Ocelot網(wǎng)關(guān)中實(shí)現(xiàn)IdentityServer4密碼模式”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。