Connecting to cloud using C# WebDav

Sure!

string username = "";
string password = "<app_password>";
String encoded = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://cloud.nextcloud.com/");
client.DefaultRequestHeaders.Add("OCS-APIRequest", "true");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", encoded);

You pass the HttpClient:

public class WebDav
{            
private IWebDavClient _webDavClient;
            private string _username;
            public WebDav(HttpClient client, string username)
            {
                _username = username;
                _webDavClient = new WebDavClient(client);
            }

        public async Task<PropfindResponse> GetItemsAsync()
        {
            return await _webDavClient.Propfind($"remote.php/dav/files/{_username}");
        }
}
2 Likes