401 Unauthorized:无法使用C#和WebDav在Exchange 2003中验证和读取收件箱

时间:2011-12-12 20:03:53

标签: c# ssl exchange-server webdav http-status-code-401

这令我感到困惑。我有两个交换服务器。一个我可以读得很好,另一个我不能。前者有SSL,后者没有。

使用该收件箱工作的正确凭据连接并读取SSL服务器上的特定收件箱。我的连接看起来像这样:

destination=https://myssqlserver.myorg.com/exchange/fish/Inbox&username=myorg.com\fish&password=FishPassword&SubmitCreds=Log+On&forcedownlevel=0&trusted=0

当我尝试连接到NON-SSL盒并从不同的收件箱中读取时,同样的事情(完全相同的代码)失败了(我也有凭据)

destination=http://mynonsslserver.myorg.com/exchange/mammel/Inbox&username=myorg.com\mammel&password=MammelPassword&SubmitCreds=Log+On&forcedownlevel=0&trusted=0

奇怪的是,如果我只是获取非SSL连接的信息并将其输入到IE的URL中,它会提示用户名和密码,我可以通过OWA看到收件箱就好了。

http://mynonsslserver.myorg.com/exchange/mammel/Inbox/?username=myorg.com\mammel&password=MammelPassword

那么为什么我在使用此设置的非SSL服务器上失败了?我不明白。

这是我的验证码。它在应该得到响应的行上失败了:

public void Authenticate()
    {

      // Create the web request body:
      var credentials = "destination={0}&username={1}\\{2}&" + "password={3}&SubmitCreds=Log+On&forcedownlevel=0&trusted=0";
      var body = string.Format(credentials, _uri, _domain, _conn.Username, _conn.Password);
      var bytes = Encoding.UTF8.GetBytes(body);

      // Create the web request:
      var request = (HttpWebRequest)WebRequest.Create(_conn.AuthURI);
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
      request.CookieContainer = new CookieContainer();
      request.ContentLength = bytes.Length;

      // Create the web request content stream:
      using (var stream = request.GetRequestStream())
      {
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();
      }

      // Get the response & store the authentication cookies:
      // THIS LINE FAILS
      var response = (HttpWebResponse) request.GetResponse();

      if (response.Cookies.Count < 2)
        throw new AuthenticationException("Login failed. Is the login / password correct?");

      _cookieContainer = new CookieContainer();
      _cookies = new CookieCollection();
      foreach (Cookie myCookie in response.Cookies)
      {
        _cookieContainer.Add(myCookie);
        _cookies.Add(myCookie);
      }

      response.Close();

      Console.WriteLine("----------------- AUTH REQUEST COMPLETE -----------------");
    }

0 个答案:

没有答案