【发布时间】:2016-02-28 06:34:31
【问题描述】:
每次我尝试调用服务器时,都会收到错误代码:ErrorConnectionFailed 和 Connection 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