【问题标题】:EWS exception : "Connection failed. Try later."EWS 异常:“连接失败。稍后再试。”
【发布时间】:2016-02-28 06:34:31
【问题描述】:

每次我尝试调用服务器时,都会收到错误代码:ErrorConnectionFailedConnection failed. Try later. 消息。

我怀疑这是因为service 的凭据为空。虽然我不知道为什么。如果我使用我的 Windows 帐户登录名和密码手动创建凭据,它可以正常工作:new WebCredentials(login, password, domain);

我有一个可以正常工作的控制台程序(见下文),但它不在网站上。

static void Main(string[] args)
{
    var service = GetContextualService(email);
    EmailMessage email = EmailMessage.Bind(service, new ItemId(validEmailId));
    Console.ReadKey();
}

private static ExchangeService GetContextualService(string email)
{
    ExchangeService service = new ExchangeService();

    // I don't even need credentials on a Console program to make it work
    //service.Credentials = new WebCredentials(CredentialCache.DefaultCredentials);
    //service.UseDefaultCredentials = true;
    service.AutodiscoverUrl(email, RedirectionUrlValidationCallback);

    return service;
}

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

即使使用new WebCredentials(CredentialCache.DefaultCredentials); 在网站上使用,它也会返回异常。 (见下文)

private ExchangeService GetContextualService(string email)
{
    ExchangeService service = new ExchangeService();

    service.Credentials = new WebCredentials(CredentialCache.DefaultCredentials);
    //service.UseDefaultCredentials = true;

    service.AutodiscoverUrl(email, RedirectionUrlValidationCallback);

    return service;
}

[HttpPost]
public List<InternetMessageHeader> GetMailHeader(JObject data)
{
    ExchangeService service = GetContextualService(data.GetValue("email").Value<string>());
    ItemId id = new ItemId(data.GetValue("mailId").Value<string>());
    // EXCEPTION BELOW
    EmailMessage email = EmailMessage.Bind(service, id);

    return email.InternetMessageHeaders.ToList();
}

为什么对 EWS 的任何调用都会返回异常?

为什么它在控制台程序上运行良好而不是在 Web 服务器上运行良好?

欢迎提出任何想法!

【问题讨论】:

  • 如果它使用 EWSEditor 工作,我想这意味着它使用 EWS XML 工作。那么是什么使它无法使用托管 API 工作呢?
  • 实际上 EWSEditor 使用 EWS Managed API,该错误通常是限制响应您尝试一次打开多少个项目?
  • 我更新了问题。如果您需要更多详细信息,请告诉我。
  • 这很可能是 kerberos 委派问题,请参阅 blogs.msdn.com/b/emeamsgdev/archive/2012/07/26/…
  • 默认情况下,控制台程序以与当前登录用户相同的权限运行,而网站(我在这里假设 IIS)以不同的权限运行,可能是 IUSER。这意味着默认凭据通常不会对外部服务进行身份验证。

标签: c# asp.net iis exchangewebservices


【解决方案1】:

严格根据您发布的代码,我只能说,当您调用 AutoDiscoverUrl() 时,它可能不会与您需要与 EWS 通信的同一台服务器通信。虽然通常 AD 和 EWS 在 CAS 上,但有可能(我认为)获得指向其他服务器的 EWS URL。我已经有一段时间没有使用 EWS 编辑器中的代码了,但如果它不调用托管 API,它的 AD 可能会略有不同。我建议在尝试 Bind() 之前调用 EWS URL,看看是否可以将其粘贴到浏览器中。 (它会重定向你 OWA,但连接会被证明。)我还会在重定向回调中调用 AD URL,这样你就知道你在和谁说话。抱歉,我无法提供更多帮助。

【讨论】:

  • 看起来问题出在 凭据为空这一事实。对此的想法是,它可能是由 Visual Studio 以管理员身份启动时引起的。问题是当 Visual Studio 以管理员身份启动时,我不知道如何解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
相关资源
最近更新 更多