Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to write a web service client in c# which the webservice is Java Axis 1.4. Axis service requires the Authorization: Basic Base64EncodedToken header value in the HTTP Headers. I can't find a way to set this header in standart ways of consuming web services in visual studio.net, like normal WSDL generated refernce nor with WSE3.0

I can't use WCF as the project is developed using .net 2.0.

Is there any way to do this ?

share|improve this question

6 Answers 6

It seems the original author has found their solution, but for anyone else who gets here looking to add actual custom headers, if you have access to mod the generated Protocol code you can override GetWebRequest:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
  System.Net.WebRequest request = base.GetWebRequest(uri);
  request.Headers.Add("myheader", "myheader_value");
  return request;
}

Make sure you remove the DebuggerStepThroughAttribute attribute if you want to step into it.

share|improve this answer
    
This was exactly what I needed. The Web Service was returning 500 instead of 401 for Unauthorized, so the credentials were never sent using the approach in the accepted answer. While I feel dirty modifying generated code, this allowed me to force send them with the first request. See west-wind.com/weblog/posts/243915.aspx. –  g . Dec 24 '10 at 11:40
1  
Hey, that's seems like exactly what I need. but where should I put this function? –  Roee Gavirel Dec 19 '11 at 14:43
    
but can't a header addition have multiple comma seperated values in the value portion for the header you're adding? That too is very important to know such as for OAuth Authorization params –  CoffeeAddict Feb 28 '12 at 20:56
2  
I've put this in a partial class along with the "reference.cs" file, so that if it is regnerated it will not be lost. –  boomhauer Jun 22 '12 at 19:09

Are we talking WCF here? I had issues where the service calls were not adding the http authorization headers, wrapping any calls into this statement fixed my issue.

  using (OperationContextScope scope = new OperationContextScope(RefundClient.InnerChannel))
  {
            var httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " +
            Convert.ToBase64String(Encoding.ASCII.GetBytes(RefundClient.ClientCredentials.UserName.UserName + ":" +
            RefundClient.ClientCredentials.UserName.Password));
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

            PaymentResponse = RefundClient.Payment(PaymentRequest);
   }

This was running SOAP calls to IBM ESB via .NET with basic auth over http or https.

hope this helps someone because i had massive issues finding a solution online.

share|improve this answer
1  
This was actually my problem. Somehow when you set the credentials in the proxy, it doesn't work. Thanks! –  Tawani Dec 2 '11 at 16:03

If you want to send a custom HTTP Header (not a SOAP Header) then you need to use the HttpWebRequest class the code would look like:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("Authorization", token);

You cannot add HTTP headers using the visual studio generated proxy, which can be a real pain.

share|improve this answer
    
"You cannot add HTTP headers using the visual studio generated proxy" Is this still the case? –  chaostheory Apr 9 '10 at 23:40
    
I can add the headers like this but how do i get them back in the WCF Service? –  Joshy Sep 27 '13 at 20:04

I find this code and is resolve my problem.

http://arcware.net/setting-http-header-authorization-for-web-services/

protected override WebRequest GetWebRequest(Uri uri)
{
    // Assuming authValue is set from somewhere, such as the config file
    HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);
    request.Headers.Add("Authorization", string.Format("Basic {0}", authValue));
    return request;
}
share|improve this answer
1  
Hey, that's seems like exactly what I need. but where should I put this function? –  Roee Gavirel Dec 19 '11 at 14:43
    
I also don't know where to put it and I'm using .net 4.0. My service is generated differently than other examples I find –  Pertinent Info Jul 23 at 21:02

See Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication and search on "Using specific credentials". Also see the MSDN docs for the CredentialCache Class.

share|improve this answer
    
Thanks but this doesn't add the credentials to HTTP Headers –  UmutKa May 22 '09 at 13:59
    
Ah. Do you want to bypass the authentication handshake and force the Authentication header to be included on the first request? Then see also eggheadcafe.com/articles/20051104.asp ... he describes a method there on how to do that. –  Chris W. Rea May 22 '09 at 14:21
    
That doesn't also add the necessary headers. Check my answer. –  UmutKa May 22 '09 at 15:25

Here is what worked for me:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
        HttpWebRequest request;
        request = (HttpWebRequest)base.GetWebRequest(uri);
        NetworkCredential networkCredentials =
        Credentials.GetCredential(uri, "Basic");
        if (networkCredentials != null)
        {
            byte[] credentialBuffer = new UTF8Encoding().GetBytes(
            networkCredentials.UserName + ":" +
            networkCredentials.Password);
            request.Headers["Authorization"] =
            "Basic " + Convert.ToBase64String(credentialBuffer);
            request.Headers["Cookie"] = "BCSI-CS-2rtyueru7546356=1";
            request.Headers["Cookie2"] = "$Version=1";
        }
        else
        {
            throw new ApplicationException("No network credentials");
        }
        return request;
}

Don't forget to set this property:

service.Credentials = new NetworkCredential("username", "password");  

Cookie and Cookie2 are set in header because java service was not accepting the request and I was getting Unauthorized error.

share|improve this answer
    
I got rid of above overload and only this code did all magic: serviceClient.CookieContainer = new CookieContainer(); –  Attiq Rehman Aug 7 '13 at 16:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.