Basic Authenitcation is a - not so secure - method of authenticating users to a web server.
The username and password are sent in the HTTP request header with Base64 "encryption" which is as good as plain text.
However at some point you may have or may want to do just that, either because there is still no trust between organizations (believe me, the world is good
) or just because its too easy and other methods are way too hard to implement.
Now there you are, how do you add a header to a HttpRequest in plain c#?
Follow these steps:
- Generate the proxy using WSDL.EXE. Search MSDN on how to do that.
- Add this function to the partial class:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);
NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
if (networkCredentials != null)
byte credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName +":" + networkCredentials.Password);
// Note the space after Basic
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
throw new ApplicationException("No network credentials");
- within the client using your proxy add the following:
// these are NOT my real credentials
NetworkCredential netCredential = new NetworkCredential("Elvis", "Graceland");
Uri uri = new Uri(svc.Url);
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
svc.Credentials = credentials;
// set PreAuthenticate as it is checked !!!
svc.PreAuthenticate = true;