【发布时间】:2015-05-11 01:58:03
【问题描述】:
我正在编写一个程序,以使用 C# 中的 EWS 从 Exchange 服务器转储大量邮箱的内容。使用提琴手我注意到我发送的每个请求都会建立一个新的连接(隧道),并进行一个新的身份验证过程(使用协商)。每个请求都会调用我的 ServerCertificateValidationCallback。
如果我在 Fiddler 中启用“重用服务器连接”选项,则连接仅在握手期间创建,并被重用于所有请求(节省大量时间)。
通过获取 EWS 源并修改我发现的请求,如果我在请求对象上启用“UnsafeAuthenticatedConnectionSharing”而不是重新使用连接(额外的隧道和 ServerCertificateValidationCallbacks 消失),但每个请求仍然需要完整的握手身份验证。这是因为当我尝试使用交换 cookie 时,服务器会发回 401。
有什么方法可以重复使用我的服务器连接和身份验证?
public class EwsExchange
{
static int Main(string[] args)
{
sslCertCheckCount = 0;
ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidation;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new NetworkCredential(args[1], args[2]);
service.Url = new Uri(args[0] + @"/EWS/exchange.asmx");
service.KeepAlive = true;
service.PreAuthenticate = true;
//service.UnsafeAuthenticatedConnectionSharing = true;
Folder folder = Folder.Bind(service, WellKnownFolderName.Inbox, new PropertySet(FolderSchema.Id, FolderSchema.DisplayName));
FindItemsResults<Item> res = folder.FindItems(new ItemView(int.MaxValue));
return 0;
}
public static bool ServerCertificateValidation(Object obj, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
Console.WriteLine(String.Format(" ****************** ServerCertificateValidation - count: {0}. ****************** ", ++sslCertCheckCount));
return true;
}
static int sslCertCheckCount;
}
谢谢!
【问题讨论】:
-
您好,欢迎您!如果您还没有,您应该使用tour,我建议您阅读如何提供clear and minimal 示例。你的代码很长,有一个最小的例子会有所帮助!
标签: c# exchange-server exchangewebservices