Connecting to WebDAV Server Using .NET API

Authenticating

The WebDAV client API supports all types of authentication supported by .Net Framework: Basic, Digest, NTLM, Kerberos.

string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolder folder = null;
try
{
    folder = await session.GetFolderAsync(new Uri("https://server/Products"));
}
catch (UnauthorizedException)
{
    Console.WriteLine("Incorrect user name or password.");
}
catch (NotFoundException)
{
    Console.WriteLine("Folder not found.");
}

if (folder != null)
{
    Console.WriteLine(folder.LastModified.ToString());
}

To authenticate against Active directory accounts use:

session.Credentials = new NetworkCredential("User1", "pwd", "DOMAIN");

To utilize the username, password, and domain of the user who is currently logged in or impersonated use:

session.Credentials = CredentialCache.DefaultCredentials;

To build your own custom authentication scheme, you must implement IAuthenticationModule Interface and register it with AuthenticationManager.Register method.

Connection Limits

By default every .Net application can open only 2 connections with each domain. To open more connections simultaneously you must increase connection limit: 

HttpClientHandler handler = new HttpClientHandler() {
    MaxConnectionsPerServer = 40
};
WebDavSession session = new WebDavSession(license, handler);

Connecting Over SSL / HTTPS

If WebDAV server returns incorrect server certificate the API will throw WebDavException with a description “The underlying connection was closed: Could not establish a trust relationship for the SSL/TLS secure channel.” To avoid this exception and accept non-trusted server certificates set the ServerCertificateCustomValidationCallback in HttpClientHandler prior to calling client API methods:

HttpClientHandler handler = new HttpClientHandler() {
    ServerCertificateCustomValidationCallback = (httpRequestMessage, certificate, chain, sslPolicyErrors) =>
    {
        Console.WriteLine("Issued by: " + certificate.Issuer);
        Console.WriteLine("Issued to: " + certificate.Subject);
        Console.WriteLine("Valid until: " + certificate.GetExpirationDateString());

        if (sslPolicyErrors == SslPolicyErrors.None)
            Console.WriteLine("Valid server certificate");
        return true;
    }
};
WebDavSession session = new WebDavSession(license, handler);
IFolder item = await session.GetFolderAsync(new Uri("https://server:8080/"));

Setting Proxy Connection

WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://server1:8888");
proxy.Credentials = new NetworkCredential("User1", "pwd");
HttpClientHandler handler = new HttpClientHandler()
{
    Proxy = proxy,
    UseProxy = true
};
WebDavSession session = new WebDavSession(license, handler);
IFolder folder = await session.GetFolderAsync(new Uri("http://server2:9876/"));

Next Article:

Managing Items Hierarchy on a WebDAV Server Using .NET API