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 = session.OpenFolder(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:
ServicePointManager.DefaultConnectionLimit = 40;
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 ServerCertificateValidationCallback in ServicePointManager prior to calling client API methods:
using System.Net.Security; using System.Security.Cryptography.X509Certificates; ... private static bool ValidateCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors 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; } ... ServicePointManager.ServerCertificateValidationCallback =new RemoteCertificateValidationCallback(ValidateCertificate); WebDavSession session = new WebDavSession(license); IFolder item = session.OpenFolder(new Uri("https://server:8080/"));
Setting Proxy Connection
WebDavSession session = new WebDavSession(license); WebProxy proxy = new WebProxy(); proxy.Address = new Uri("http://server1:8888"); proxy.Credentials = new NetworkCredential("User1", "pwd"); session.Proxy = proxy; IFolder folder = session.OpenFolder(new Uri("http://server2:9876/"));