Last active
March 10, 2017 09:40
-
-
Save maestrow/7bf9f4458d4b77e77c80fbfd4b4b68da to your computer and use it in GitHub Desktop.
HTTP Client Authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using (HttpClient client = new HttpClient()) | |
| { | |
| client.DefaultRequestHeaders.Accept.Add( | |
| new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | |
| client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", | |
| Convert.ToBase64String( | |
| System.Text.ASCIIEncoding.ASCII.GetBytes( | |
| string.Format("{0}:{1}", "login", "password")))); | |
| using (HttpResponseMessage response = client.GetAsync( | |
| "https://url").Result) | |
| { | |
| response.Dump(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("login:password")) | |
| client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var credentials = new NetworkCredential("login", "password", "domain"); | |
| var handler = new HttpClientHandler { Credentials = credentials }; | |
| using (var http = new HttpClient(handler)) | |
| { | |
| var response = http.GetAsync("https://url").Result; | |
| response.Dump(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://url"); | |
| request.Method = "GET"; | |
| request.UseDefaultCredentials = false; | |
| request.PreAuthenticate = false; | |
| request.AllowAutoRedirect = false; | |
| request.Credentials = new NetworkCredential("login", "password", "domain"); | |
| HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |
| response.Dump(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment