Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Windows Integrated Authentication #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions WebDAVClient/WebDAVClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,35 @@ public String Domain
set { domain = value; }
}

private bool useIntegratedAuthentication = false;
/// <summary>
/// Enable integrated Windows authentication
/// </summary>
public bool UseIntegratedAuthentication
{
get { return useIntegratedAuthentication; }
set { useIntegratedAuthentication = value; }
}

Uri getServerUrl(String path, Boolean appendTrailingSlash)
{
String completePath = basePath;
if (path != null)
{
completePath += path.Trim('/');
completePath += path.Trim('/');
}

if (appendTrailingSlash && completePath.EndsWith("/") == false) { completePath += '/'; }

if(port.HasValue) {
return new Uri(server + ":" + port + completePath);
if (port.HasValue)
{
return new Uri(server + ":" + port + completePath);
}
else {
return new Uri(server + completePath);
else
{
return new Uri(server + completePath);
}

}
#endregion

Expand Down Expand Up @@ -195,8 +207,8 @@ void FinishList(IAsyncResult result)
if (file.Length > 0)
{
// Want to see directory contents, not the directory itself.
if (file[file.Length-1] == remoteFilePath || file[file.Length-1] == server) { continue; }
files.Add(file[file.Length-1]);
if (file[file.Length - 1] == remoteFilePath || file[file.Length - 1] == server) { continue; }
files.Add(file[file.Length - 1]);
}
}
}
Expand Down Expand Up @@ -391,13 +403,13 @@ public class RequestState
void HTTPRequest(Uri uri, string requestMethod, IDictionary<string, string> headers, byte[] content, string uploadFilePath, AsyncCallback callback, object state)
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

/*
* The following line fixes an authentication problem explained here:
* http://www.devnewsgroups.net/dotnetframework/t9525-http-protocol-violation-long.aspx
*/
System.Net.ServicePointManager.Expect100Continue = false;

// If you want to disable SSL certificate validation
/*
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
Expand All @@ -407,22 +419,31 @@ void HTTPRequest(Uri uri, string requestMethod, IDictionary<string, string> head
return validationResult;
};
*/
// The server may use authentication
if (user != null && pass != null)

// Check if we must use the Windows Integrated Authentication
if (useIntegratedAuthentication)
{
NetworkCredential networkCredential;
if (domain != null)
{
networkCredential = new NetworkCredential(user, pass, domain);
}
else
httpWebRequest.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
httpWebRequest.PreAuthenticate = true;
}
else
{
// The server may use user/password authentication
if (user != null && pass != null)
{
networkCredential = new NetworkCredential(user, pass);
NetworkCredential networkCredential;
if (domain != null)
{
networkCredential = new NetworkCredential(user, pass, domain);
}
else
{
networkCredential = new NetworkCredential(user, pass);
}
httpWebRequest.Credentials = networkCredential;
// Send authentication along with first request.
httpWebRequest.PreAuthenticate = true;
}
httpWebRequest.Credentials = networkCredential;
// Send authentication along with first request.
httpWebRequest.PreAuthenticate = true;
}
httpWebRequest.Method = requestMethod;

Expand Down