Connect to an Axis 1 Web Service with Basic Authentication

Share

Recently we had to integrate one of our .NET applications with an Axis Web Service using basic authentication over an https connection.

In order to connect to the Web Service and pass the username and password for the basic authentication, we needed to build a proxy for the service and add the authentication header manually. The code for the proxy class is listed bellow. It simply overrides the GetWebRequest method of the service and adds the credentials encoded in base64.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 class ProjectRepositoryProxy : ProjectRepositoryServiceSoapService
  {
    protected override WebRequest GetWebRequest(Uri uri)
    {
      HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);
      request.PreAuthenticate = true;
      request.Headers.Add("Authorization: Basic " + EncodeCredentialsToB64((NetworkCredential)Request.Credentials));
      return request;
    }
    static private string EncodeCredentialsToB64(NetworkCredential credentials)
    {
      byte[] toEncodeAsBytes
        = System.Text.ASCIIEncoding.ASCII.GetBytes(credentials.UserName + ":" + credentials.Password);
      string returnValue
        = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }

Calling a method of the web service, requires setting the Credentials property of the web service to an instance of NetworkCredentials with username and password.

1
2
3
  ProjectRepositoryProxy service = new ProjectRepositoryProxy();
service.Credentials = new System.Net.NetworkCredential("user","pass");
string   response = service.getModifiedProjects();

Another tip that might prove useful when working with web services over https connections, is how to override certificate validation in case you have problems with it during development phase. For this you must simply register a Callback that validates the certificate like in the code bellow.

1
2
3
4
5
6
 ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);
...
 private static bool customXertificateValidation(object sender, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors error)
        {
            return true;
        }

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

1 thought on “Connect to an Axis 1 Web Service with Basic Authentication”
  • Luis Molina says:

    hi, I need more code from this sample, I dont know what the ProjectRepositoryServiceSoapService comes from.

    thanks in advance..

    January 9, 2012 at 3:00 pm

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close