Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Accessing the GAL (Global Address List) from ASP.NET

0.00/5 (No votes)
3 Dec 2008 1  
This article focuses on accessing the GAL details from Microsoft Exchange in an ASP.NET application.

Introduction

Access the GAL (Global Address List) information from a web application, is it possible? Obviously, yes! There are few articles available on the web which list different methods to do so. Let us see one of the methods in detail.

Background

Occasionally, there would be some requirement to access the Microsoft Exchange properties from a web application. Accessing the Global Address List is one among them. There are three different ways to achieve the Outlook contacts from within a .NET web application. This article describes all the three in brief.

For my case, using WebDAV suited best. The WebDAV method of accessing will return only the details of the contacts available in the logged-in user's contact list, and not from the GAL. After Googling, I found an article discussing about the OWA way of finding GAL entries. The OWA has a command called GalFind, which is used to find the limited set of fields from the GAL. The general syntax for GalFind, in a URL query string is:

http://YourCompanyExchangeServer/public/?Cmd=galfind&fn=rajga

This request will give the response containing all the GAL entries having the first name "rajga". This article describes all the possible search parameters. In this article, I tried to combine both the approaches, and provided an elaborate solution written in C# .NET.

Using the code

GAL entries can be queried using the GalFind command in OWA. If we send a simple HttpWebRequest with the URL "http://YourCompanyExchangeServer/public/?Cmd=galfind&fn=rajga" and valid network credentials, we would expect a response containing the matching list. But, the response would be an HTML page having a place holder to enter the credentials, because the server is expecting a valid authentication cookie.

Hence, to make this request valid, we need to attach a valid authentication cookie along with the request. The code below shows an example of sending the HttpWebRequest, to find the first name match.

string ResponseString = string.Empty;
string Server = "YourServerName";
string NetworkUserName = "YourValidUserName";
string NetworkUserPassword = "YourValidPassword";
string NetworkUserDomain = "YourValidDomain";
NetworkCredential _oNetworkCredential = 
  new NetworkCredential(NetworkUserName, 
  NetworkUserPassword, NetworkUserDomain);
CookieCollection _oCookieCollection = 
  GetOWAAuthCookies(Server, _oNetworkCredential);

/* Check for the First Name */
string uri = string.Format("{0}/Public/?Cmd=galFind&fn={1}", 
                           Server, "rajga");
HttpWebRequest _oHttpWebRequest = 
  HttpWebRequest.Create(uri) as HttpWebRequest;
_oHttpWebRequest.Credentials = _oNetworkCredential;
_oHttpWebRequest.CookieContainer = new CookieContainer();
_oHttpWebRequest.CookieContainer.Add(_oCookieCollection);
_oHttpWebRequest.UserAgent = "Mozilla/4.0(compatible;MSIE 6.0; " + 
                             "Windows NT 5.1; SV1; .NET CLR 1.1.4322; " + 
                             ".NET CLR 2.0.50727; InfoPath.1)";
using (HttpWebResponse _oHttpWebResponse = 
       _oHttpWebRequest.GetResponse() as HttpWebResponse)
{
    StreamReader _oStreamReader = 
      new StreamReader(_oHttpWebResponse.GetResponseStream());
    ResponseString = _oStreamReader.ReadToEnd();
    _oStreamReader.Close();
    _oHttpWebResponse.Close();
}

Response.Write(ResponseString);

The code in bold is used to get the authentication cookie. This method uses the WebDAV sample shown in this article to call owaauth.dll to get the authentication cookie. The method is shown below:

private CookieCollection GetOWAAuthCookies(string server, NetworkCredential credentials)
{
    string authURI = string.Format("{0}/exchweb/bin/auth/owaauth.dll", 
                                   server, credentials.UserName);
    byte[] bytes = 
      Encoding.UTF8.GetBytes(string.Format("destination={0}/exchange/" + 
                             "{1}&username={2}\\{1}&password={3}", 
                             server, credentials.UserName, credentials.Domain, 
                             credentials.Password));
    HttpWebRequest request = WebRequest.Create(authURI) as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencode";
    request.CookieContainer = new CookieContainer();
    request.ContentLength = bytes.Length;
    request.AllowAutoRedirect = false;
    using (Stream requestStream = request.GetRequestStream())
        requestStream.Write(bytes, 0, bytes.Length);

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        CookieCollection _oCookieCollection = response.Cookies;
        response.Close();
        return _oCookieCollection;
    }
}

Executing this code will return the GAL entries matching the first name; the same can be implemented for last name, display name, and alias name as well.

Points of interest

Initially, in my _oHttpWebRequest object, I did not set the UserAgent property. The _oHttpResponse object returned me a whole lot of HTML as a response. Then, after setting the UserAgent to "Mozilla/4.0(compatible;MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)", I got a valid XML response.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here