//Functions that runs an HTTP request and returns a response as string. public async static Task<string> RunHttpRequestWithStringReturn(Uri uri) { var httpFilter = new HttpBaseProtocolFilter(); httpFilter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent; var httpClient = new HttpClient(httpFilter); HttpResponseMessage result = new HttpResponseMessage(); try { result = await httpClient.GetAsync(uri); } catch { result.StatusCode = HttpStatusCode.Forbidden; return await Task.FromResult("Server timed out"); ; } if (!result.IsSuccessStatusCode) { return await Task.FromResult("Server timed out"); } return result.Content.ToString(); }
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); } }
private static async Task Main() { HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); }; //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; //clientHandler.cert //var cert = new X509Certificate2("fred.pfx", "apples"); HttpClientHandler handler = new HttpClientHandler(); X509Certificate2 certificate = new X509Certificate2("certificate.pfx", "secret"); handler.ClientCertificates.Add(certificate); if (certificate == null) { Console.WriteLine("certificado ta nulo"); } handler.ClientCertificates.Add(certificate); //HttpClient client = new HttpClient(handler); // discover endpoints from metadata var client = new HttpClient(clientHandler); 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 = "43253244523456", ClientSecret = "secret6", Scope = "Enfermeiro" }); //tokenResponse.AccessToken.c Console.WriteLine(tokenResponse.ToString()); if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); return; } Console.WriteLine(tokenResponse.Json); Console.WriteLine("\n\n"); // call api var apiClient = new HttpClient(clientHandler); apiClient.SetBearerToken(tokenResponse.AccessToken); //apiClient. var response = await apiClient.GetAsync("https://localhost:6001/Funcionario/Consultas13145232242"); if (!response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode); } else { var content = await response.Content.ReadAsStringAsync(); Console.WriteLine("Deu certo????"); Console.WriteLine(content); Console.WriteLine(JsonConvert.DeserializeObject(content)); } }
private 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; } #region request token using Client Credentials Console.WriteLine("--------------request token using Client Credentials--------------"); // request token using Client Credentials 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)); } #endregion #region request token using Passwords (alice) Console.WriteLine("--------------request token using Passwords (alice) --------------"); // request token using Passwords var tokenResponseAlice = await client.RequestPasswordTokenAsync(new PasswordTokenRequest { Address = disco.TokenEndpoint, ClientId = "ro.client", ClientSecret = "secret", UserName = "******", Password = "******", Scope = "api1" }); if (tokenResponseAlice.IsError) { Console.WriteLine(tokenResponseAlice.Error); return; } Console.WriteLine(tokenResponseAlice.Json); Console.WriteLine("\n\n"); // call api var apiClientAlice = new HttpClient(); apiClientAlice.SetBearerToken(tokenResponseAlice.AccessToken); var responseAlice = await apiClientAlice.GetAsync("http://localhost:5001/identity"); if (!responseAlice.IsSuccessStatusCode) { Console.WriteLine(responseAlice.StatusCode); } else { var content = await responseAlice.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content)); } #endregion #region request token using Passwords (bob) Console.WriteLine("--------------request token using Passwords (bob)--------------"); // request token using Passwords var tokenResponseUsingBob = await client.RequestPasswordTokenAsync(new PasswordTokenRequest { Address = disco.TokenEndpoint, ClientId = "ro.client", ClientSecret = "secret", UserName = "******", Password = "******", Scope = "api1" }); if (tokenResponseUsingBob.IsError) { Console.WriteLine(tokenResponseUsingBob.Error); return; } Console.WriteLine(tokenResponseUsingBob.Json); Console.WriteLine("\n\n"); // call api var apiClientBob = new HttpClient(); apiClientBob.SetBearerToken(tokenResponseUsingBob.AccessToken); var responseBob = await apiClientBob.GetAsync("http://localhost:5001/identity"); if (!responseBob.IsSuccessStatusCode) { Console.WriteLine(responseBob.StatusCode); } else { var content = await responseBob.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content)); } #endregion }
private static async Task MainAsync() { // discover endpoints from metadata // IdentityModel includes a client library to use with the discovery endpoint. // This way you only need to know the base-address of IdentityServer // - the actual endpoints addresses can be read from the metadata: var disco = await DiscoveryClient.GetAsync("http://localhost:5000"); if (disco.IsError) { Console.WriteLine(disco.Error); return; } // request token TokenClient tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret"); var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1"); if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); return; } Console.WriteLine(tokenResponse.Json); Console.WriteLine("\n\n"); // call api HttpClient client = new HttpClient(); // To send the access token to the API you typically use the HTTP Authorization header. This is done using the SetBearerToken extension method: client.SetBearerToken(tokenResponse.AccessToken); var response = await client.GetAsync("http://localhost:5001/identity"); if (!response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode); } else { string content = await response.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content)); } //requesting token using password grant // The token will now contain "sub" claim. The presence or absence of sub claim lets the Api distiguish between calls on behalf of clients and calls on behalf of users var tokenClientPasswordGrant = new TokenClient(disco.TokenEndpoint, "ro.client", "secret"); var tokenResponsePasswordGrant = await tokenClientPasswordGrant.RequestResourceOwnerPasswordAsync("alice", "password", "api1"); if (tokenResponsePasswordGrant.IsError) { Console.WriteLine(tokenResponsePasswordGrant.Error); return; } Console.WriteLine(tokenResponsePasswordGrant.Json); Console.WriteLine("\n\n"); // call api // To send the access token to the API you typically use the HTTP Authorization header. This is done using the SetBearerToken extension method: client.SetBearerToken(tokenResponsePasswordGrant.AccessToken); var responsePasswordGrant = await client.GetAsync("http://localhost:5001/identity"); if (!responsePasswordGrant.IsSuccessStatusCode) { Console.WriteLine(responsePasswordGrant.StatusCode); } else { string content = await responsePasswordGrant.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content)); } }