Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

Reuse EWS connection and negotiate authentication for Exchange

$
0
0

I am writing a program to dump the contents of numerous mailboxes from an Exchange server using EWS in C#. Using fiddler I noticed that each request I send makes a new connection (tunnel), with a new authentication process being undertaken (using negotiate). My ServerCertificateValidationCallback gets called for every request.

If I enable the option in Fiddler to "reuse server connections" than the connection is only created during handshaking, and is re-used for all requests (saving lots of time).

By getting the EWS source and modifying the requests I found if I enable "UnsafeAuthenticatedConnectionSharing" on the request objects than the connection is re-used (extra tunnels & ServerCertificateValidationCallbacks disappear), but each request still requires the full handshake authentication. This is because the server sends back a 401 when ever I try and use the exchange cookie.

Is there any way I can re-use my server connection & authentication?

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;
}

Thanks!


Viewing all articles
Browse latest Browse all 7132

Trending Articles