1. 程式人生 > >.NET Core IdentityServer4實戰 第Ⅳ章-整合密碼登陸模式

.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來加以複雜認證,但是用密碼也不太可靠,抽時間我們說下授權碼模式,說一說它們的區別與實現。