views:

6328

answers:

6

Hello,

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 ?

Thanks, Umut

A: 

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.

Chris W. Rea
Thanks but this doesn't add the credentials to HTTP Headers
UmutKa
Ah. Do you want to bypass the authentication handshake and force the Authentication header to be included on the first request? Then see also http://www.eggheadcafe.com/articles/20051104.asp ... he describes a method there on how to do that.
Chris W. Rea
That doesn't also add the necessary headers. Check my answer.
UmutKa
+2  A: 

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.

John Hunter
"You cannot add HTTP headers using the visual studio generated proxy"Is this still the case?
chaostheory
A: 

This is what i was looking for.

http://mark.michaelis.net/Blog/CallingWebServicesUsingBasicAuthentication.aspx

UmutKa
That was John's answer a while ago.
jro
Hmmm... this isn't working for me. PreAuth still fails to send the headers on the first request.
Rick Strahl
+1  A: 

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.

Simon RC
+1  A: 

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;
    }

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

A: 

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; }

Valdemar Carneiro