I had a task where I needed to add basic authorization to a soap call and I found that I couldn’t set the network credentials nor the authorization header. In order to do this you have to create another class that inherits the proxy class and then override the GetWebRequest method. Here is sample code:
using System;
using System.Net;
using System.Text;
namespace MyNamespace
{
public class MyProxyOverride : TheProxy
{
string _url = null;
string _username = null;
string _password = null;
public MyProxyOverride(string url, string userName, string password)
{
_url = url;
_username = userName;
_password = password;
}
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
var request = (HttpWebRequest)base.GetWebRequest(new Uri(_url));
var netCredential = new NetworkCredential(_username, m__password);
var networkCredentials = netCredential.GetCredential(new Uri(_url), "Basic");
if (networkCredentials != null)
{
byte[] credentialBuffer = new UTF8Encoding().GetBytes(
networkCredentials.UserName + ":" +
networkCredentials.Password);
request.Headers["Authorization"] =
"Basic " + Convert.ToBase64String(credentialBuffer);
}
else
{
throw new ApplicationException("Missing authentication information.");
}
return request;
}
}
}