Arch2Arch Tab BEA.com
Syndicate this blog (XML)

Setting Basic Auth Headers with .NET for PAPI-WS

Bookmark Blog Post

del.icio.us del.icio.us
Digg Digg
DZone DZone
Furl Furl
Reddit Reddit

Clinton Davidson's Blog | August 2, 2006   3:28 PM | Comments (0)


First, generate the stubs using the .NET tools. As in Java, the credentials have been copied from the file /ptprocess/1.5/tomcat/conf/tomcat-users.xml. The Web service endpoint, and the basic auth username and password were put into a base class called PageBase, but you can supply them any way that suits your needs.

Second, add the following code to the generated constructor for ProcessServiceService, and override the method GetWebRequest(Uri uri):

public ProcessServiceService()
{
  //set the web service endpoint- this could also be put into a setter method
  this.Url = PageBase.WEB_SERVICE_ENDPOINT;
  //add the basic auth credentials
  //the credentials could also be supplied in a setter method
  //or in an alternate constructor
  //be sure to add
  //using System.Net;
  //to recognize the class NetworkCredential
  NetworkCredential networkCredential = new NetworkCredential(PageBase.BASIC_AUTH_USERNAME,     PageBase.BASIC_AUTH_PASSWORD);
  Uri uri = new Uri(this.Url);
  ICredentials credentials =
  networkCredential.GetCredential(uri, "Basic");
  this.Credentials = credentials;
  //set preauthenticate to true, or the
  //credentials will not be supplied on the first request, and the //request will fail
  this.PreAuthenticate = true;
}

//despite the documentation, it is not enough to set preauthenticate
//to true. You must also override the GetWebRequest method
//to add the basic auth headers or they will not be supplied
//on the first request
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
  HttpWebRequest request;
  request = (HttpWebRequest)base.GetWebRequest(uri);

  if (PreAuthenticate)
  {
    NetworkCredential networkCredentials =
    Credentials.GetCredential(uri, "Basic");

    if (networkCredentials != null)
    {
      //be sure to add
      //using System.Text;
     //to recognize the class UTF8Encoding
      byte[] credentialBuffer = new UTF8Encoding().GetBytes(
      networkCredentials.UserName + ":" +
      networkCredentials.Password);
      request.Headers["Authorization"] ="Basic " + Convert.ToBase64String(credentialBuffer);
     }
    else
     {
      throw new ApplicationException("No network credentials");
     }
  }
  return request;
}

Comments

Comments are listed in date ascending order (oldest first) | Post Comment



Only logged in users may post comments. Login Here.

Powered by
Movable Type 3.31