Example #1
0
        static async Task Main(string[] args)
        {
            // discovery 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.ToString());

            // call the api
            var apiClient = new HttpClient();

            client.SetBearerToken(tokenResponse.AccessToken);

            var response = await client.GetAsync("http://localhost:5001/identity");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            var content = await response.Content.ReadAsStringAsync();

            Console.WriteLine(JArray.Parse(content));
        }
Example #2
0
        static async Task Main(string[] args)
        {
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("https://localhost:5001");

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

            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);

            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);

            var response = await apiClient.GetAsync("https://localhost:6001/identity");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(JArray.Parse(content));
            }

            Console.ReadLine();
        }
Example #3
0
        private static async Task UseClientCredential()
        {
            // discover endpoints from metadata
            var client = new HttpClient();

            var disco = await client.GetDiscoveryDocumentAsync(ID4ServerUri);

            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 uri = $"{APIServerUri}/identity";

            Console.WriteLine($"Request api endpoint: {uri}");
            Console.WriteLine("Response:");

            var response = await apiClient.GetAsync(uri);

            await ConsoleOutput(response);
        }
Example #4
0
        static async Task Main(string[] args)
        {
            var client = new HttpClient();

            //this will get the discovery document from the identity server and work based on it
            var discoveryDocument = await client.GetDiscoveryDocumentAsync("https://localhost:5005");

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

            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = discoveryDocument.TokenEndpoint,
                ClientId     = "Client",
                ClientSecret = "Secret",
                Scope        = "api"
            });

            //checking the token
            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }

            Console.WriteLine(tokenResponse.Json);

            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);
            var response = await apiClient.GetAsync("https://localhost:5001/Identity/");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(JArray.Parse(content));
            }
        }
Example #5
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            // discover endpoints from metadata
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("https://localhost:5001");

            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;
            }

            // call api
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);

            var response = await apiClient.GetAsync("https://localhost:44370/identity");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(JsonSerializer.Serialize(content));
            }
        }
Example #6
0
        public static async Task <int> Main(string[] args)
        {
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("https://cloudgenidentity.azurewebsites.net/");

            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return(-1);
            }
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = disco.TokenEndpoint,

                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "afcpayroll"
            });

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

            client = new HttpClient();
            client.SetBearerToken(tokenResponse.AccessToken);

            var response = await client.GetAsync("https://localhost:44379/api/Default");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(JArray.Parse(content));
            }


            Console.ReadLine();
            return(0);
        }
        public async static void ConnectIdentityServer()
        {
            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        = "api1"
            });

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

            Console.WriteLine(tokenResponse.Json);

            //call api
            var clientApi = new HttpClient();

            clientApi.SetBearerToken(tokenResponse.AccessToken);

            var response = await clientApi.GetAsync("http://localhost:5001/identity");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(content);
            }
        }
Example #8
0
        static async Task <TokenResponse> GetClientCredentialsTokenAsync(HttpClient client, DiscoveryDocumentResponse disco)
        {
            // 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(tokenResponse);
        }
Example #9
0
        static async Task Main(string[] args)
        {
            //创建http客户端
            //IdentityModel这个类库,给httpclient加了扩展方法,实现identity的相关操作
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("https://localhost:50012");

            if (disco.IsError)
            {
                System.Console.WriteLine(disco.Error);
                return;
            }
            //请求identityServer来获取token 这里采用的是credential的方式类似于账号密码
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest {
                Address = disco.TokenEndpoint,

                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "api1",
            });

            if (tokenResponse.IsError)
            {
                System.Console.WriteLine(tokenResponse.Error);
                return;
            }
            System.Console.WriteLine(tokenResponse.Json);
            //发送access token去请求api 通常是使用http授权头,可以通过SetBearerToken扩展方法实现
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);
            var response = await apiClient.GetAsync("https://localhost:60011/identity");

            if (!response.IsSuccessStatusCode)
            {
                System.Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                System.Console.WriteLine(content);
            }
        }
Example #10
0
        static async Task <int> Main(string[] args)
        {
            // Discover endpoints from metadata
            var identityServerClient = new HttpClient();
            var disco = await identityServerClient.GetDiscoveryDocumentAsync("http://localhost:5000");

            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return(1);
            }
            // Request token
            var tokenResponse = await identityServerClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "api1"
            });

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return(2);
            }
            Console.WriteLine(tokenResponse.Json);
            Console.WriteLine("\n\n");
            // Call API
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);
            var apiResponse = await apiClient.GetAsync("http://localhost:5002/identity");

            if (!apiResponse.IsSuccessStatusCode)
            {
                Console.WriteLine(apiResponse.StatusCode);
                return(3);
            }
            var content = await apiResponse.Content.ReadAsStringAsync();

            Console.WriteLine(JArray.Parse(content));
            return(0);
        }
Example #11
0
        static void Main(string[] args)
        {
            var client = new HttpClient();
            var disco  = client.GetDiscoveryDocumentAsync("http://localhost:5000");

            if (disco.Result.IsError)
            {
                Console.WriteLine(disco.Result.Error);
                return;
            }
            var tokenResponse = client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest()
            {
                Address      = disco.Result.TokenEndpoint,
                ClientId     = "client",
                ClientSecret = "123",
                Scope        = "api1"
            });

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


            // call api
            var clieTtwo = new HttpClient();

            client.SetBearerToken(tokenResponse.Result.AccessToken);

            var response = client.GetAsync("http://localhost:5001/identity");

            if (!response.Result.IsSuccessStatusCode)
            {
                Console.WriteLine(response.Result.StatusCode);
            }
            else
            {
                var content = response.Result.Content.ReadAsStringAsync();
                Console.WriteLine(JArray.Parse(content.Result));
            }
        }
Example #12
0
        private static async Task ClientCredentials(DiscoveryResponse disco)
        {
            var client = new HttpClient();
            // request access token
            //获取token
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint, //http://localhost:5000/connect/token
                ClientId     = "client",            //客户名字
                ClientSecret = "secret",            //密码
                Scope        = "api1 openid ",      //需要访问的API,或者说需要获得的API访问权限,需要当前客户下有对应的Scope才会赋予权限,重点:多个scope用空格分开
            });

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                Console.ReadKey();
                return;
            }
            //获得JWT token
            Console.WriteLine(tokenResponse.Json);
            Console.WriteLine("\n\n");

            // 调用API接口
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);
            //API站点需要与颁发token的站点沟通,以确认token的有效性
            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));
            }

            Console.ReadKey();
        }
        static private async Task GetToken(HttpClient client, DiscoveryDocumentResponse disco)
        {
            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);
        }
Example #14
0
        private static async Task MainAsync()
        {
            var client = new HttpClient();

            var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = "https://localhost:5001/connect/token",

                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "api1"
            });

            if (response.IsError)
            {
                throw new Exception(response.Error);
            }

            var token = response.AccessToken;

            Console.WriteLine($"Token: ${response.Json}");

            Console.WriteLine("*******************************");

            var client2 = new HttpClient();

            client2.SetBearerToken(token);

            var response2 = await client2.GetAsync("https://localhost:6001/identity");

            if (!response2.IsSuccessStatusCode)
            {
                Console.WriteLine(response2.StatusCode);
            }
            else
            {
                var content = await response2.Content.ReadAsStringAsync();

                Console.WriteLine(content);
                //Console.WriteLine(content);
            }
        }
Example #15
0
        private static async Task <TokenResponse> GetCredentialToken(HttpClient client, DiscoveryResponse disco)
        {
            // request token
            TokenResponse tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "client",
                ClientSecret = "secret",

                Scope = "api1"
            });

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

            Console.WriteLine(tokenResponse.Json);
            Console.WriteLine("\n\n");
            return(tokenResponse);
        }
        public static void Main(string[] args)
        {
            var client            = new HttpClient();
            var discoveryDocument = client.GetDiscoveryDocumentAsync("http://localhost:5005").GetAwaiter().GetResult();

            if (HasError(discoveryDocument))
            {
                return;
            }

            var tokenResponse = client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest()
            {
                Address = discoveryDocument.TokenEndpoint,

                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "api1"
            }).GetAwaiter().GetResult();

            if (HasError(tokenResponse))
            {
                return;
            }

            Console.WriteLine(tokenResponse.Json);

            client.SetBearerToken(tokenResponse.AccessToken);

            var response = client.GetAsync("http://localhost:5001/identity").GetAwaiter().GetResult();

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                Console.WriteLine(JArray.Parse(content));
            }
        }
Example #17
0
        private static async Task PerformRequest()
        {
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("http://localhost:5000");

            if (disco.IsError)
            {
                return;
            }

            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "OrdersAPI"
            });

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

            client = new HttpClient();
            client.SetBearerToken(tokenResponse.AccessToken);

            var response = await client.GetAsync("http://localhost:5001/orders");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine("Ok!");
            }
        }
Example #18
0
        static async Task Main(string[] args)
        {
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("https://localhost:44350");

            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return;
            }
            // request token
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = disco.TokenEndpoint,

                ClientId     = "AF8754D8-796C-456F-91DF-1B0D21EB085E",
                ClientSecret = "testtest",
                Scope        = "Tenogy.Api:Access2"
            });

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

            Console.WriteLine(tokenResponse.Json);


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

            await CallApi(client, "access");

            await CallApi(client, "scopes/scope");
            await CallApi(client, "scopes/scope1");
            await CallApi(client, "scopes/scope2");
        }
Example #19
0
        static async Task Main(string[] args)
        {
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("https://localhost:44330");

            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
            }

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

                ClientId     = "platformApiClient",
                ClientSecret = "SuperSecretPassword",
                Scope        = "quasar.read:*/*"
            });

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

            var factory = new ConnectionFactory {
                Password = tokenResponse.AccessToken
            };
// "guest"/"guest" by default, limited to localhost connections

            var conn = factory.CreateConnection();


            Console.WriteLine(tokenResponse.Json);
            Console.Read();
        }
Example #20
0
        static void Main(string[] args)
        {
            var client = new HttpClient();
            var disco  = client.GetDiscoveryDocumentAsync("https://localhost:5000").Result;

            if (!disco.IsError)
            {
                var token = client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest()
                {
                    Address      = disco.TokenEndpoint,
                    ClientId     = "apiClient",
                    ClientSecret = "secret",
                    Scope        = "api1"
                }).Result;

                if (!token.IsError)
                {
                    Console.WriteLine(token.Json);

                    client = new HttpClient();
                    client.SetBearerToken(token.AccessToken);

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

            Console.ReadLine();
        }
Example #21
0
        static async Task Main(string[] args)
        {
            var protocolClient = new HttpClient();

            var disco = await protocolClient.GetDiscoveryDocumentAsync("https://demo.identityserver.io");

            if (disco.IsError)
            {
                throw new Exception(disco.Error);
            }

            var tokenResponse = await protocolClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "client",
                ClientSecret = "secret"
            });

            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);

            Console.WriteLine(await apiClient.GetStringAsync("http://localhost:3308/test"));
        }
        /// <summary>
        /// 根据客户端凭证获取AccessToken
        /// </summary>
        /// <returns></returns>
        private static async Task <string> GetAccessToken()
        {
            var client = new HttpClient();

            // 获取导航文件
            DiscoveryDocumentResponse disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");

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

            // 客户端凭证模式获取token
            var request = new ClientCredentialsTokenRequest
            {
                Address = disco.TokenEndpoint,

                ClientId     = "clientCredentials.client",
                ClientSecret = "secret",

                //Scope = "MyApiResourceScope1",//欲进入的资源域,不指定则为ids中为该客户端配置的允许访问的所有资源域;指定的话,则获取到的token只能用来进入指定的资源域
            };
            TokenResponse tokenResponse = await client.RequestClientCredentialsTokenAsync(request);

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

            Console.WriteLine("token:");
            Console.WriteLine(tokenResponse.Json);

            return(tokenResponse.AccessToken);
        }
Example #23
0
        private static async Task <string> GetAccessToken()
        {
            var httpClient = new HttpClient();

            var openIdConnectEndPoint = await httpClient.GetDiscoveryDocumentAsync("https://localhost:5000");

            var accessToken = await httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = openIdConnectEndPoint.TokenEndpoint,
                ClientId     = "oauthClient",
                ClientSecret = "SuperSecretPassword",
                Scope        = "api1.read",
            });

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

            Console.WriteLine(accessToken.Json);

            return(accessToken.AccessToken);
        }
Example #24
0
        static async Task <TokenResponse> RequestTokenAsync()
        {
            var client = new HttpClient();

            var disco = await client.GetDiscoveryDocumentAsync(Constants.Authority);

            if (disco.IsError)
            {
                throw new Exception(disco.Error);
            }

            var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "54aafa4c-c720-447c-a60a-0e5ccd14782d",
                ClientSecret = "wQJb1xk26WEKlVAT8js4IOI36CLXKAKj0o1Urfnx"
            });

            if (response.IsError)
            {
                throw new Exception(response.Error);
            }
            return(response);
        }
Example #25
0
        private static async Task Main()
        {
            try
            {
                // discover endpoints from metadata
                var client = new HttpClient();
                //var ids4Url = "http://101.200.182.225:53362";
                //// discover endpoints from metadata
                //var discoveryClient = new DiscoveryClient(ids4Url) { Policy = { RequireHttps = false } };
                //var disco = await discoveryClient.GetAsync();
                var disco = await client.GetDiscoveryDocumentAsync("http://localhost:53362");

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

                //request token
                var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
                {
                    Address      = disco.TokenEndpoint,
                    ClientId     = "business-app",
                    ClientSecret = "1q2w3e*",

                    Scope = "InternalGateway BaseService"
                });

                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:51186/api/app/cs-machine-shift-report");
                //if (!response.IsSuccessStatusCode)
                //{
                //    Console.WriteLine(response.StatusCode);
                //}
                //else
                //{

                //    //var content = await response.Content.ReadAsStringAsync();
                //    ////Console.WriteLine(content);
                //    //string baseAddr = "http://localhost:51186";
                //    //string path = "http://localhost:51186/BusinessService"; //"Bearer " + tokenResponse.AccessToken;
                //    //string auth = "Bearer " + tokenResponse.AccessToken;
                //    //HttpFactory httpFactory = new HttpFactory();
                //    //AbsHttpHelper helper = httpFactory.CreateHttpHelper("post", baseAddr, auth, path, true, null);
                //    //string rs = helper.GetResult();
                //    //Console.WriteLine(rs);


                //}
                using (var httpClient = new HttpClient())
                {
                    httpClient.SetBearerToken(tokenResponse.AccessToken);
                    var response1 = await httpClient.GetAsync("http://localhost:51186/api/business/lt-machine-shift-report");

                    //var response1 = await httpClient.GetAsync("http://localhost:51186/api/business/lt-machine-shift-report");
                    if (response1.IsSuccessStatusCode)
                    {
                        System.Console.WriteLine(await response1.Content.ReadAsStringAsync());
                    }
                    else
                    {
                        Console.WriteLine(response1.StatusCode);
                    }
                }


                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
Example #26
0
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Hello Console!");

            // DISCO - Discovery Endpoint
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
            {
                Address = "http://host.docker.internal:5000",
                Policy  = { RequireHttps = false }
            });

            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                Console.ReadKey(true);
                return;
            }

            // Richiesta dell'AccessToken passando i parametri richiesti, usando il client M2M
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "m2m-client",
                ClientSecret = "thisissosecret",
                Scope        = "user.basic",
            });

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                Console.ReadKey(true);
                return;
            }
            Console.WriteLine(tokenResponse.Json);
            Console.WriteLine("\n\n");


            // Richiesta dell'API usando l'AccessToken come Authorization nell'header
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);
            // microservice 1
            var response = await apiClient.GetAsync("http://localhost:15100/numbers/random");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(content);
            }
            Console.WriteLine("\n\n");
            // microservice 2
            response = await apiClient.GetAsync("http://localhost:15200/strings/hello");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(content);
            }
            Console.WriteLine("\n\n");
            // microservice 2 - claims
            response = await apiClient.GetAsync("http://localhost:15200/claims");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(content);
            }

            Console.ReadKey(true);
        }
Example #27
0
        static async Task Main(string[] args)
        {
            // discover endpoints from metadata
            var client = new HttpClient();
            var disco  = await client.GetDiscoveryDocumentAsync("http://localhost:5000");

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

            // user password
            var tokenPasswordTokenAsync = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "intro",
                ClientSecret = "secret",

                UserName = "******",
                Password = "******",
                Scope    = "api1"
            });

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

            Console.WriteLine(tokenPasswordTokenAsync.Json);

            // 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);

            // call api
            client.SetBearerToken(tokenResponse.AccessToken);

            var response = await client.GetAsync("http://localhost:5001/weatherforecast");

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                Console.WriteLine(JArray.Parse(content));
            }
        }
        static async Task Start()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");

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

                    var tokenResource = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
                    {
                        Address = disco.TokenEndpoint,

                        ClientId     = "clientid",
                        ClientSecret = "secret",
                        Scope        = "myApi"
                    });


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

                    Console.WriteLine(tokenResource.Json);

                    await Send(tokenResource.AccessToken);

                    async Task Send(string token)
                    {
                        using (var client2 = new HttpClient())
                        {
                            client2.SetBearerToken(token);

                            var response = await client2.GetAsync($"http://localhost:5001/api/values/{DateTime.Now.ToString("yyyyMMddHHmmss")}");

                            if (!response.IsSuccessStatusCode)
                            {
                                Console.WriteLine(response.StatusCode);
                                return;
                            }
                            else
                            {
                                var content = await response.Content.ReadAsStringAsync();

                                Console.WriteLine($"result:{content}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"There was an exception: {ex.ToString()}");
            }
        }
Example #29
0
        private static async Task Main()
        {
            // discover endpoints from metadata
            var client = new HttpClient();

            var disco = await client.GetDiscoveryDocumentAsync(IdpUri); // IDP

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

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

                Scope = "######"
            });

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

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

            var token = tokenResponse.AccessToken;

            var apiClient = new HttpClient();

            apiClient.SetBearerToken(token);

            // GET Location lockers
            var response = await apiClient.GetAsync($"{ApiUri}/api/e-commerce/location/locker"); // API

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                var lockers = JsonConvert.DeserializeObject <IEnumerable <ECommerceLocationLockersModel> >(content);
                var locker  = lockers.FirstOrDefault();
                Console.WriteLine(locker?.LockerNumber);

                var order = await LoadLocker(locker?.QrCode, "SAMPle Order No", "Sample User Id", token);

                if (order == null)
                {
                    return;
                }

                Console.WriteLine(order.OtpCode, order.OrderId);

                var confirm = await Confirm(order.OrderId, token);

                if (confirm == null)
                {
                    return;
                }

                Console.WriteLine(confirm.ConfirmLink, confirm.DeliveryNumber, confirm.LockerNumber, token);
            }
        }
Example #30
0
        private static async Task MainAsync()
        {
            // discover Identity Server 4's endpoints via its discovery document
            var client = new HttpClient(); // Note: Both DiscoveryClient and TokenClient were deprecated
            DiscoveryDocumentResponse disco = await client.GetDiscoveryDocumentAsync(IdendityServer4Address);

            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return; // without "discovery document" we can not go on
            }

            // obtain the access token, for this client
            TokenResponse tokenResponse = await client.RequestClientCredentialsTokenAsync(
                new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = "client",
                ClientSecret = "secret",
                Scope        = "WebAPI"
            }
                );

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return; // without "access token" we can not go on
            }

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

            // Ready to consume the WebAPI
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);

            var customerInfo = new StringContent(   // construct the payload for adding new customer
                JsonConvert.SerializeObject(
                    new
            {
                Id        = 12,
                FirstName = "Kenny",
                LastName  = "Doe"
            }),
                Encoding.UTF8,
                "application/json");

            // post to the WebAPI to add new customer
            HttpResponseMessage createCustomerResponse = await apiClient.PostAsync(WebApiEndPoint, customerInfo);

            if (!createCustomerResponse.IsSuccessStatusCode)
            {
                Console.WriteLine(createCustomerResponse.StatusCode);
            }
            else
            {
                Console.WriteLine("Added new Custeromer");
            }

            // from the WebAI, get a list of the customers
            HttpResponseMessage getCustomerReponse = await apiClient.GetAsync(WebApiEndPoint);

            if (!getCustomerReponse.IsSuccessStatusCode)
            {
                Console.WriteLine(getCustomerReponse.StatusCode);
            }
            else
            {
                string content = await getCustomerReponse.Content.ReadAsStringAsync();

                Console.WriteLine(JArray.Parse(content));
            }

            Console.ReadLine();
        }