Click here to Skip to main content
5,787,682 members and growing! (17,804 online)
Email Password   helpLost your password?
Enterprise Systems » Microsoft Exchange » General     Intermediate License: The Code Project Open License (CPOL)

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

By Rajganesh Mountbatton

This article focuses on accessing the GAL details from Microsoft Exchange in an ASP.NET application.
C# (C# 1.0, C# 2.0, C# 3.0, C#), .NET (.NET, .NET 2.0), WebForms, ASP.NET, Dev

Posted: 4 Dec 2008
Updated: 4 Dec 2008
Views: 1,624
Bookmarked: 11 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.34 Rating: 2.82 out of 5
0 votes, 0.0%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 66.7%
5

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)

About the Author

Rajganesh Mountbatton


A Developer from India, having more than 3 years of experience in .NET; mostly writing code in C#, JavaScript and AJAX
Occupation: Software Developer
Location: India India

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralMy vote of 2mvpJohn Simmons / outlaw programmer5:19 5 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Dec 2008
Editor: Smitha Vijayan
Copyright 2008 by Rajganesh Mountbatton
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project