.NET Core IdentityServer4實戰 第Ⅳ章-集成密碼登陸模式


  回顧下ClientCredentials模式,在ReSourceApi中定義了我們公開服務,第三方網站想要去訪問ReSourceApi則需要在身份驗證服務中獲取toekn,根據token的內容,硬編碼去訪問公開服務(ResApi),這個還是非常簡單的,但!仔細思考下,我們在客戶端當中設置了對應的身份驗證服務中心的地址,那么也就是可以有多對多的情況,當然我們的第三方網站無需多言去關注這些。

public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.Authority = "https://localhost:5000"; options.RequireHttpsMetadata = false; options.ApiName = "api"; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }

  下面咱說下密碼模式,這個模式安全級別比ClientCredentials高得多,第一步我們需要修改一下我們的Config文件.然后第二步就是添加我們的TestUser對象.

public static IEnumerable<Client> GetClients()
        {
            return new List<Client> { new Client() { ClientId = "client", AllowedGrantTypes = GrantTypes.ClientCredentials,//客戶端登陸模式 ClientSecrets ={ new Secret("secret".Sha256()) }, AllowedScopes = {"api"} }, new Client() { ClientId = "pwdClient", AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,//密碼授權登陸模式 ClientSecrets ={ new Secret("secret".Sha256()) }, AllowedScopes = {"api"} } }; }

第二部TestUser對象由IdentityServer4.Test 給我們提供了,我們引入就ok,然后添加該方法用於測試.

public static List<TestUser> GetTestUsers()
        {
            return new List<TestUser> { new TestUser() { SubjectId = "1", Username = "zara", Password = "112233" } }; }

當然,你還需要將測試數據注入到core中,我們需要修改下原來的Stratup.cs類.

services.AddIdentityServer()//將Idserer DI到.netcore
                .AddDeveloperSigningCredential()
                .AddInMemoryApiResources(Config.GetResource())//添加公開服務
                .AddInMemoryClients(Config.GetClients())//客戶端模擬數據
                .AddTestUsers(Config.GetTestUsers());//用戶測試數據

 下面我們用postMan來測試一下,先用原來的客戶端模式,看,我們對客戶端模式不會影響。 

現在我們再試一下密碼登陸模式,首先獲取token!

我們再去ReSourceApi中進行測試,OK,沒問題!

現在我們創建一個客戶端,用於硬編碼的密碼登陸。

 

using IdentityModel.Client;
using System; using System.Net.Http; namespace ThirdPartySolucation { public static class passWordLogin { public static void Login() { var diso = DiscoveryClient.GetAsync("https://localhost:5000").Result; if (diso.IsError) { Console.WriteLine(diso.Error); } var tokenClient = new TokenClient(diso.TokenEndpoint, "pwdClient", "secret"); var tokenResponse = tokenClient.RequestResourceOwnerPasswordAsync("zara","112233").Result; if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); } else { Console.WriteLine(tokenResponse.Json); } HttpClient httpClient = new HttpClient(); httpClient.SetBearerToken(tokenResponse.AccessToken); var response = httpClient.GetAsync("http://localhost:5001/api/values").Result; if (response.IsSuccessStatusCode) { Console.WriteLine(response.Content.ReadAsStringAsync().Result); } Console.WriteLine(); } } }

啟動,結果如下:

 

如果需要不做secret驗證,在Config中添加該參數:

new Client()
                {
                    ClientId = "pwdClient", AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets ={ new Secret("secret".Sha256()) }, RequireClientSecret = false, AllowedScopes = {"api"} }

上圖是基本的客戶端登陸模式,而密碼模式呢,則會在獲取獲取token中Body上下文中加入username,password來加以復雜認證,但是用密碼也不太可靠,抽時間我們說下授權碼模式,說一說它們的區別與實現。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM