1.客户端模式
var client = new HttpClient(); var response = client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = "http://localhost:5000/connect/token", ClientId = "client", ClientSecret = "secret", Scope = "api" }).Result; Console.WriteLine("=============================Token=============================="); Console.WriteLine(response.AccessToken ?? response.Error); Console.WriteLine("=============================Token=============================="); // 调用API client.SetBearerToken(response.AccessToken); var responses = client.GetAsync("http://localhost:5001/weatherforecast/get").Result; if (response.IsError) { Console.WriteLine(response.HttpStatusCode); } else { Console.WriteLine("=============================服务返回值=============================="); var content = responses.Content.ReadAsStringAsync().Result; Console.WriteLine(content); } Console.WriteLine("Hello World!"); Console.ReadKey();
2.密码模式
var client = new HttpClient(); var response = client.RequestPasswordTokenAsync(new PasswordTokenRequest { Address = "http://localhost:5000/connect/token", ClientId = "apiClientPassword", ClientSecret = "apiSecret", Scope = "secretapi", UserName="123", Password="123" }).Result; Console.WriteLine("=============================Token=============================="); Console.WriteLine(response.AccessToken ?? response.Error); Console.WriteLine("=============================Token=============================="); // 调用API client.SetBearerToken(response.AccessToken); var responses = client.GetAsync("http://localhost:5001/weatherforecast/get").Result; if (response.IsError) { Console.WriteLine(response.HttpStatusCode); } else { Console.WriteLine("=============================服务返回值=============================="); var content = responses.Content.ReadAsStringAsync().Result; Console.WriteLine(content); } Console.WriteLine("Hello World!"); Console.ReadKey();
3.刷新refresh_token
var client = new HttpClient(); var response = client.RequestPasswordTokenAsync(new PasswordTokenRequest { Address = "http://localhost:5000/connect/token", ClientId = "apiClientPassword", ClientSecret = "apiSecret", Scope = "secretapi", UserName="123", Password="123" }).Result; Console.WriteLine("=============================Token=============================="); Console.WriteLine(response.AccessToken ?? response.Error); Console.WriteLine("=============================Token=============================="); // 调用API client.SetBearerToken(response.AccessToken); var responses = client.GetAsync("http://localhost:5001/weatherforecast/get").Result; if (response.IsError) { Console.WriteLine(response.HttpStatusCode); } else { Console.WriteLine("=============================服务返回值=============================="); var content = responses.Content.ReadAsStringAsync().Result; Console.WriteLine(content); } Console.WriteLine("Hello World!"); Console.ReadKey();
标签:Console,NetCore,client,Result,WriteLine,var,response,Identityserver4,客户端 From: https://www.cnblogs.com/microsoft-zh/p/17987148