Forced Basic Authentication in WebRequest

I am integrating a web service that will use HTTP POST to request and receive data. Remote server requires basic authentication in accordance with RFC 2617

My authentication attempts fail.

In this case, it doesn’t work, even if I attach the 'NetworkCredential' object to the 'Credentials' property of the HttpWebRequest object, no authentication information is sent in the header, even if I set "PreAuthenticate" = true.

What am I missing?

// a piece is used

NetworkCredential netCredential = new NetworkCredential(" uid", "pwd"); Uri uri = new Uri("http://address of services"); ICredentials credentials = netCredential.GetCredential(uri, "Basic"); objRegistration.Credentials = credentials; objRegistration.PreAuthenticate = true; 
+47
authentication web-services webrequest
May 04 '10 at 10:46
source share
4 answers

I just found this very handy little piece of code to do exactly what you need. It adds the authorization header to the code manually, without waiting for the server to call.

 public void SetBasicAuthHeader(WebRequest request, String userName, String userPassword) { string authInfo = userName + ":" + userPassword; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); request.Headers["Authorization"] = "Basic " + authInfo; } 

Use it like this:

 var request = WebRequest.Create("http://myserver.com/service"); SetBasicAuthHeader(request, userName, password); var response = request.GetResponse(); 
+107
Jul 16 2018-10-16T00:
source share

The improvement is a bit in response to the samuel-jack. Instead of using the default encoding, "ISO-8859-1" should be used as indicated in this answer What encoding should I use for basic HTTP authentication?

Thus, the code will look like this:

 public void SetBasicAuthHeader(WebRequest request, String userName, String userPassword) { string authInfo = userName + ":" + userPassword; authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo)); request.Headers["Authorization"] = "Basic " + authInfo; } 
+7
Dec 17 '15 at 16:54
source share

Here is my solution for OAuth. The value is in the json variable.

 var myUri = new Uri(fullpath); var myWebRequest = WebRequest.Create(myUri); var myHttpWebRequest = (HttpWebRequest)myWebRequest; myHttpWebRequest.PreAuthenticate = true; myHttpWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken); myHttpWebRequest.Accept = "application/json"; var myWebResponse = myWebRequest.GetResponse(); var responseStream = myWebResponse.GetResponseStream(); if (responseStream == null) return null; var myStreamReader = new StreamReader(responseStream, Encoding.Default); var json = myStreamReader.ReadToEnd(); responseStream.Close(); myWebResponse.Close(); 
+2
Feb 08 '15 at 1:36
source share

I found this question looking for an answer, the answer to this question works, but is not flexible, so if you want to improve the .NET way to do this.

 Uri uri = new Uri("http://address of services"); HttpWebRequest objRegistration = (HttpWebRequest)WebRequest.Create(url); CredentialCache credentials = new CredentialCache(); NetworkCredential netCredential = new NetworkCredential(" uid", "pwd"); credentials.Add(uri, "Basic", netCredential); objRegistration.Credentials = credentials; 

You can replace "Basic" with "Digest", "NTLM" or "Negotiate", and also allow you to add several types to the cache.

+2
May 7 '15 at 12:16
source share



All Articles