日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)

發布時間:2025/7/14 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前寫了一篇文章:《IdentityServer4 實現 OpenID Connect 和 OAuth 2.0》

上面這篇文章雖然詳細,但都是點到為止的介紹,并沒有實際應用的示例,所以,后面在真正去實現的時候,踩到了自己之前種下的很多坑。

業務場景:前后端分離項目,前端調用后端業務服務需要授權訪問(提供access_token),access_token在用戶登錄的時候(用戶名和密碼登錄),由授權中心生成access_token并返回給前端,這樣前端就可以拿到access_token,去調用后端業務服務了。

一開始,我使用的GrantTypes.Implicit模式,登錄頁面在授權中心,登錄成功之后會跳到callback.htm#access_token=*頁面,前端調用使用oidc-client組件,然后獲取access_token,當時使用還沒什么,現在覺得真是一團亂麻,前后端分離的項目,在授權中心居然把登錄頁面放在服務中了,但我后面還是沒有意識到GrantTypes.Implicit的問題,而是嘗試在這種模式下,寫HTTP Post請求授權中心(提供用戶名和密碼),然后沒然后,一團糟。。。

使用 IdentityServer4 實現上面的業務場景,其實很簡單,只要使用GrantTypes.GrantTypes.ResourceOwnerPassword模式,就可以了。

Startup.ConfigureServices配置代碼:

var builder = services.AddIdentityServer(); builder.AddTemporarySigningCredential()//.AddInMemoryIdentityResources(Config.GetIdentityResources()).AddInMemoryApiResources(Config.GetApiResources()).AddInMemoryClients(new List<Client>{new Client{ClientId = "client_id_1",AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,AllowOfflineAccess = true,AccessTokenLifetime = 3600 * 6, //6小時SlidingRefreshTokenLifetime = 1296000, //15天ClientSecrets ={new Secret("secret".Sha256())},AllowedScopes ={IdentityServerConstants.StandardScopes.OfflineAccess, "api1"}}}); builder.AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();

ResourceOwnerPasswordValidator示例代碼:

public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator {private readonly IUserService _userService;public ResourceOwnerPasswordValidator(IUserService userService){_userService = userService;}public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context){var userId = await _userService.Login(context.UserName, context.Password);if (userId != 0){context.Result = new GrantValidationResult(userId.ToString(), OidcConstants.AuthenticationMethods.Password);}} }

使用ResourceOwnerPasswordValidator的作用,就是自定義用戶登錄的用戶名密碼判斷,而不是使用 IdentityServer4 的TestUser。

請求示例:IdentityServer4 Token Endpoint

獲取access_token請求示例:

刷新access_token請求示例:

也可以服務端進行請求,示例代碼:

private async Task<TokenResponse> GetToken(string clientId, string clientSecret, string grantType, string userName, string password, string scope) {var client = new DiscoveryClient($"http://localhost:5001");client.Policy.RequireHttps = false;var disco = await client.GetAsync();var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);return await tokenClient.RequestResourceOwnerPasswordAsync(userName, password, scope); }private async Task<TokenResponse> GetRefreshToken(string clientId, string clientSecret, string grantType, string refreshToken) {var client = new DiscoveryClient($"http://localhost:5001");client.Policy.RequireHttps = false;var disco = await client.GetAsync();var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);return await tokenClient.RequestRefreshTokenAsync(refreshToken); }

參考資料:

  • ASP.NET CORE IDENTITYSERVER4 RESOURCE OWNER PASSWORD FLOW WITH CUSTOM USERREPOSITORY
  • IResourceOwnerPasswordValidator isn't being called?
  • Client scope definition with IResourceOwnerPasswordValidator
  • Resource Owner Password Validation
  • IdentityServer4 .netCore RefreshToken Example
  • IdentityServer4 Refresh Tokens
  • IdentityServer4 Client
  • IdentityServer4 Discovery Endpoint

總結

以上是生活随笔為你收集整理的IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。