Click here to Skip to main content
Click here to Skip to main content

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

, 4 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajganesh Mountbatton
Technical Lead
India India
An Electronics Engineer by curricula, Software Engineer by profession. Have passion towards developing applications using Microsoft's .NET technology.
Follow on   Twitter

Comments and Discussions

 
Question[My vote of 1] Does not work with 2010 Pinmemberalex_vara5-Feb-13 18:21 
QuestionDoes this work with Exchange 2007? PinmemberKarthick791112-Oct-09 3:57 
AnswerRe: Does this work with Exchange 2007? PinmemberRajganesh Mountbatton6-Oct-09 21:50 
QuestionAccess GAL using EWS API ?? PinmemberRenata4-May-09 23:09 
GeneralMy vote of 2 PinmvpJohn Simmons / outlaw programmer5-Dec-08 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 4 Dec 2008
Article Copyright 2008 by Rajganesh Mountbatton
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid