18

I'm attempting to make requests to a client's web service (I don't know the underlying platform at the client). I've consumed the client's WSDL in Visual Studio 2010 using "Add Web Reference" and generated my proxy class (called "ContactService").

I now need to add an authorization header like the one below to my service request.

Header=Authorization & Value=Basic 12345678901234567890

(the "123456..." value above is just placeholder)

ContactService service = new ContactService();

//not sure if this is the right way - it's not working
WebClient client = new WebClient();
client.Headers.Add("Authorization", "Basic 12345678901234567890");            
service.Credentials = client.Credentials;

int contactKey = null;
try
{                
   contactKey = service.CreateContact("ABC", emailAddress, firstName, lastName, null);
}

What is the proper way of adding the authorization header to the service request?

Thank you!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mike
  • 385
  • 1
  • 3
  • 12

4 Answers4

19

The above response was on the right track, but it just had to be in a different location.

I added this to my web reference proxy class that .Net generated:

protected override WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(uri);            
        req.Headers.Add(HttpRequestHeader.Authorization,
                "Basic " + "12345678901234567890");

        return req;
    }

A Web Reference proxy class extends System.Web.Services.Protocols.SoapHttpClientProtocol. This class contains a call to System.Net.WebRequest.GetWebRequest(Uri uri). A WebRequest allow us to set specific headers on the request when the proxy class' methods are invoked.

Thanks for your help!

Mike
  • 385
  • 1
  • 3
  • 12
  • Thank you, this helped me a lot! FYI for those using VS 2008, in order to create the proxy class in VS 2008 (which won't create the proxy class in an asp.net app), I had to add the web reference to a dummy console app, and then copy the proxy class to my web app. – Kosta Sep 03 '13 at 23:09
15

There are a couple of changes to make.

Firstly, there is a handy constant HttpRequestHeader.Authorization.

Secondly, are they expecting the header to be Base64 Encoded - this is normally required for basic authentication.

WebClient.Headers.Add(HttpRequestHeader.Authorization, 
    "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("12345678901234567890")));
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Thank you! Unfortunately, this hasn't quite gotten it. I'm still seeing a "401 Unauthorized" exception. Do I need to set this on the service in some other way (to translate from "client" to "service")? – Mike Jan 23 '13 at 20:55
  • Can you use Fiddler or similar to look at your raw request to check the header is present and correct? Also, is the third party able to help diagnose what is incorrect (or do they have any documentation of what they need)? – Fenton Jan 23 '13 at 20:58
  • Using Fiddler, it appears that the Header isn't being set on the request. I'm adding the header to the WebClient and not the service proxy - is there a way of adding to the service proxy instead? – Mike Jan 23 '13 at 21:12
3

I am writing this for whom that has this problem now. As in preceding answers mentioned inheritance hierarchy goes up to WebClientProtocol class, this class has a ICredentials property, simply set this property by a NetworkCredential instance as below:

YourServis.Credentials = new NetworkCredential("UserName", "Password", "Domain");

I think taht is simplest way without changing Reference.cs or adding headers.

Ali Faradjpour
  • 302
  • 6
  • 15
0

Fairly ancient post revival but if you are consuming an old service using vs2022 the best way to add the solution code is to create a new class and inherit from the proxy class as you can't edit the proxy class directly.