【One by One系列】IdentityServer4(三)使用用户名和密码
繼續介紹IdentityServer4,我們上篇介紹了IdentityServer4實現OAuth2.0的授權方式之一的客戶端憑證,接下來我們繼續介紹OAuth2.0的另外一種授權方式密碼式,Resource Owner Password Credentials。
post請求token?grant_type=password&username=USERNAME&password=PASSWORD&client_id=CLIENT_ID&client_secret=secret
從上面url的querystring參數就可以看出來,這里主要就是需要提供用戶的用戶名和密碼,這個在傳統的項目還是比較常見
web后臺管理系統
C/S客戶端
1.更新IdentityServer
由于上篇【One by One系列】IdentityServer4(二)使用客戶端憑證(Client Credentials)保護API資源已經創建的IdentityServer項目,我們只需要IdentityServer4中注冊用戶和添加新的客戶端。
1.1 注冊用戶
客戶端憑證是沒有用戶參與的,但是密碼式不同,需要用戶輸入用戶名和密碼,自然就需要用戶數據。當然這塊內容就屬于OpenID Connect了,因為這跟身份認證相關。
我們在Config.cs里面增加用戶數據
public?static?List<TestUser>?TestUsers?=>new?List<TestUser>{new?TestUser(){SubjectId="1",Username="admin",Password="admin123456!",Claims={?new?Claim(JwtClaimTypes.Name,"RandyField"),new?Claim(JwtClaimTypes.GivenName,"Randy"),new?Claim(JwtClaimTypes.FamilyName,"Field"),new?Claim(JwtClaimTypes.Email,"xxx@qq.com"),new?Claim(JwtClaimTypes.EmailVerified,"true",ClaimValueTypes.Boolean),new?Claim(JwtClaimTypes.WebSite,"http://www.randyfield.cn"),new?Claim(JwtClaimTypes.FamilyName,"Randy"),new?Claim(JwtClaimTypes.Address,$@"四川省成都市高新區")}??????????????}};1.2 注冊身份資源
代碼如下:
public?static?IEnumerable<IdentityResource>?IdentityResources?=>new?IdentityResource[]{//必須要添加,否則報無效的scope錯誤new?IdentityResources.OpenId(),new?IdentityResources.Profile()};1.3 注冊新客戶端
代碼如下:
????????public?static?IEnumerable<Client>?Clients?=>new?Client[]{new?Client{ClientId?=?"client?app",//?no?interactive?user,?use?the?clientid/secret?for?authenticationAllowedGrantTypes?=?GrantTypes.ClientCredentials,//?secret?for?authenticationClientSecrets?={new?Secret("secret-123456".Sha256())},//?scopes?that?client?has?access?toAllowedScopes?=?{?"api1"?}},//Resource?Owner?Password?Credentials?Clientnew?Client{ClientId="client?pwd",AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,ClientSecrets={new?Secret("secret-654321".Sha256())},AllowedScopes={?"api1",IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile?}},};這里客戶端AllowedScopes除了api資源,還額外指定了用戶Identity資源
2.創建客戶端
這里我們依然使用上篇的中的客戶端控制臺程序,只是增加代碼,模擬密碼式授權
2.1 編碼-請求Idisconvery endpoint
略,與上篇相同
2.2 編碼-請求access token
????????????//?request?tokenvar?tokenResponse1?=?await?client.RequestPasswordTokenAsync(new?PasswordTokenRequest{Address?=?disco.TokenEndpoint,ClientId?=?"client?pwd",//ClientId?=?"client",ClientSecret?=?"secret-654321",Scope?=?"api1?openid?profile",UserName=?"admin",Password=?"admin123456!"});if?(tokenResponse1.IsError){Console.WriteLine(tokenResponse1.Error);return;}RequestClientCredentialsTokenAsync更換為RequestPasswordTokenAsync
請求參數ClientCredentialsTokenRequest更換為PasswordTokenRequest
其中的用戶名和密碼,就是在IdentityServer注冊的用戶
ClientId與ClientSecret就不贅述了
Scope指明了api資源和Identity資源
3.測試
啟動IdentityServer
啟動webapi
用vs啟動client
3.1 獲取access-token
我們通過http://jwt.calebb.net/解析
3.2 調用api
3.3 獲取身份信息
調用userinfo端點,獲取身份信息
長按二維碼關注點外賣,先領券 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【One by One系列】IdentityServer4(三)使用用户名和密码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021技术人新展望
- 下一篇: 【One by One系列】Identi