重新使用 EWS 连接并为 Exchange 协商身份验证

Reuse EWS connection and negotiate authentication for Exchange

我正在编写一个程序,使用 C# 中的 EWS 从 Exchange 服务器转储大量邮箱的内容。使用提琴手我注意到我发送的每个请求都会建立一个新的连接(隧道),并进行一个新的身份验证过程(使用协商)。每个请求都会调用我的 ServerCertificateValidationCallback。

如果我将 Fiddler 中的选项启用为 "reuse server connections",则连接仅在握手期间创建,并重新用于所有请求(节省大量时间)。

通过获取 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;
}

谢谢!

所以事实证明,在我修改 EWS API 以允许我在 HttpRequests 上启用 UnsafeAuthenticatedConnectionSharing 之后,我的连接和身份验证实际上被重新使用了。

在我禁用 "Tools -> Fiddler Options -> Connections -> Reuse server connections" 选项后,Fiddler 断开了我的连接。 运行 exchange 服务器机器上的 wireshark 显示当 fiddler 使用此选项捕获时禁用 FIN TCP 标志被设置,结束会话。但是没有提琴手捕获连接和会话都被重新使用。