身份服务器:快速入门教程中的System.Net.Http.HttpRequestException

时间:2019-07-08 11:10:52

标签: c# asp.net-core .net-core asp.net-core-2.0 identityserver4

因此,我正在遵循此guide来学习身份服务器。

完成所有操作后,我得到一个 System.Net.Http.HttpRequestException

这是控制台应用程序,该应用程序调用受IdentityServer保护的API。

public static async Task Main()
    {
        // discover endpoints from metadata
        var client = new HttpClient();

        var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
        if (disco.IsError)
        {
            Console.WriteLine(disco.Error);
            return;
        }

        // request token
        var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
        {
            Address = disco.TokenEndpoint,
            ClientId = "client",
            ClientSecret = "secret",

            Scope = "api1"
        });

        if (tokenResponse.IsError)
        {
            Console.WriteLine(tokenResponse.Error);
            return;
        }

        Console.WriteLine(tokenResponse.Json);
        Console.WriteLine("\n\n");

        // call api
        var apiClient = new HttpClient();
        apiClient.SetBearerToken(tokenResponse.AccessToken);

        var response = await apiClient.GetAsync("http://localhost:5001/identity");
        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine(response.StatusCode);
        }
        else
        {
            var content = await response.Content.ReadAsStringAsync();
            Console.WriteLine(JArray.Parse(content));
        }
    }

0 个答案:

没有答案