ASP.NET Core分布式项目实战

默认教学计划
1087人加入学习
(33人评价)
价格 ¥398.00
教学计划
//针对视频中的代码做的部分调整,改为最新
static async Task Main(string[] args)
        {
            var client=new HttpClient();
            var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return;
            }

            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest()
            {
                Address = disco.TokenEndpoint,
                ClientId = "client",
                ClientSecret = "secret",
                Scope = "api"
            });
            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }
            Console.WriteLine(tokenResponse.Json);
            
            client.SetBearerToken(tokenResponse.AccessToken);
            var response = await client.GetAsync("http://localhost:5001/WeatherForecast");
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine(await response.Content.ReadAsStringAsync());
            }
        }

 

[展开全文]
using System;
using System.Net.Http;
using IdentityModel.Client;

namespace ThirdPartyDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectServer();

            var token = GetToken();

            AccessApi(token);

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }

        private static void ConnectServer()
        {
            // 连接IdentityServer服务端
            var httpClient = new HttpClient();
            var disco = httpClient.GetDiscoveryDocumentAsync("http://localhost:5005").Result;
            if (disco.IsError)
            {
                Console.WriteLine(disco.Exception);
            };
        }

        private static string GetToken()
        {
            var httpClient = new HttpClient();
            var response = httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = "http://localhost:5005/connect/token",
                ClientId = "123",
                ClientSecret = "pwd"
            }).Result;

            if (response.IsError)
            {
                Console.WriteLine(response.Exception);
                return null;
            }
            else
            {
                Console.WriteLine(response.AccessToken);
                return response.AccessToken;
            }
        }

        private static void AccessApi(string token)
        {
            if (string.IsNullOrEmpty(token)) { return;}

            var httpClient = new HttpClient();
            httpClient.SetBearerToken(token);
            var response = httpClient.GetAsync("https://localhost:5001/api/values").Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            }
            else
            {
                Console.WriteLine("error");
            }
        }
    }
}

 

[展开全文]

static void Main(string[] args)
{
    //DiscoveryClient已过时了,用HttpClient代替
    //官方文档:https://identitymodel.readthedocs.io/en/latest/client/discovery.html
    HttpClient hc = new HttpClient();
    var diso = hc.GetDiscoveryDocumentAsync("http://localhost:5000").Result;

    if (diso.IsError)
    {
        Console.WriteLine(diso.Error);
    }
    else
    {
        //TokenClient已过时,用HttpClient代替
        //官方文档:https://identitymodel.readthedocs.io/en/latest/client/token.html
        var tokenResponse = hc.RequestTokenAsync(new ClientCredentialsTokenRequest
        {
            Address = diso.TokenEndpoint,
            GrantType = "client_credentials",
            ClientId = "Client1",
            ClientSecret = "secret",
            Scope = "api1"
        }).Result;
        if (tokenResponse.IsError)
        {
            Console.WriteLine(tokenResponse.Error);
        }
        else
        {
            Console.WriteLine(tokenResponse.Json);
            hc.SetBearerToken(tokenResponse.AccessToken);
            var response=hc.GetAsync("http://localhost:5001/api/values").Result;
            if (response.IsSuccessStatusCode)
            {
                string result=response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(result);
            }
            else
            {
                Console.WriteLine(response.StatusCode);
            }
        }
                
    }
    Console.ReadKey();
}

[展开全文]

doenet new console --name ThirdPartyDemo

nuget =>IdentityModel

using IdentityModel.Client;

 

var diso =DiscoveryClient,GetAsync("http://localhost:5000").Result;

 

if(diso.IsError)

{

       ...

}

 

var tokenClient=new TokenClient(diso.TokenEndpoint,"client","secret");

var tokenResponse=tokenClient.RequestClientCredentialAsync("api").Result;

 

if(tokenResponse.IsError)

{

       ...

}

 

var httpClient=new HttpClient();

httpClient.SetBearerToken(tokenResponse.AccessToken);

var response =httpClient.GetAsync("..5001..").Result;

 

if(response .IsError)

{

       ...

}

[展开全文]

授课教师

程序员

课程特色

下载资料(2)
视频(144)
讨论(1)
图文(2)