65.9K
CodeProject 正在变化。 阅读更多。
Home

从 ASP.NET 访问 GAL(全局地址列表)

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.92/5 (4投票s)

2008 年 12 月 4 日

CPOL

2分钟阅读

viewsIcon

58423

本文重点介绍如何在 ASP.NET 应用程序中从 Microsoft Exchange 访问 GAL 详细信息。

引言

从 Web 应用程序访问 GAL(全局地址列表)信息,这是否可行? 显然,是的! Web 上有一些文章列出了不同的方法。 让我们详细了解其中一种方法。

背景

有时,需要从 Web 应用程序访问 Microsoft Exchange 属性。 访问全局地址列表就是其中之一。 有三种不同的方法可以从 .NET Web 应用程序中获取 Outlook 联系人。 这篇文章简要描述了这三种方法。

就我而言,使用 WebDAV 最合适。 WebDAV 访问方法只会返回 **登录用户的联系人列表中可用的联系人详细信息,而不是来自 GAL**。 经过一番谷歌搜索,我发现一篇文章讨论了通过 OWA 查找 GAL 条目的方法。 OWA 有一个名为 GalFind 的命令,用于从 GAL 中查找有限的字段集。 GalFind 的一般语法,在 URL 查询字符串中是

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

此请求将提供包含所有名字为“rajga”的 GAL 条目的响应。这篇文章描述了所有可能的搜索参数。 在本文中,我尝试结合这两种方法,并提供了一个用 C# .NET 编写的详细解决方案。

使用代码

可以使用 OWA 中的 GalFind 命令查询 GAL 条目。 如果我们发送一个简单的 HttpWebRequest,URL 为“http://YourCompanyExchangeServer/public/?Cmd=galfind&fn=rajga”,并且具有有效的网络凭据,我们应该会收到包含匹配列表的响应。 但是,响应将是一个 HTML 页面,其中包含用于输入凭据的占位符,因为服务器需要有效的身份验证 cookie。

因此,为了使此请求有效,我们需要将有效的身份验证 cookie 附加到请求中。 下面的代码显示了发送 HttpWebRequest 以查找名字匹配项的示例。

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);

粗体代码用于获取身份验证 cookie。 此方法使用 这篇文章中显示的 WebDAV 示例来调用 owaauth.dll 以获取身份验证 cookie。 该方法如下所示

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;
    }
}

执行此代码将返回与名字匹配的 GAL 条目; 同样可以对姓氏、显示名称和别名执行此操作。

关注点

最初,在我的 _oHttpWebRequest 对象中,我没有设置 UserAgent 属性。 _oHttpResponse 对象返回给我大量 HTML 作为响应。 然后,在将 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)”后,我收到了有效的 XML 响应。

© . All rights reserved.