SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to download a document with this code :

 using (Impersonator impersonator = new Impersonator())
     siteURL += "?NoRedirect=true";
     System.Net.WebClient _WebClient = new System.Net.WebClient();
     // Downloads the resource with the specified URI to a local file.
     _WebClient.DownloadFile(siteURL, saveName);

But i get an error 401 unothaurized access. Do you have any idea where it come froms ? Thanks

share|improve this question
But what if credentialcache is empty at all? Suppose IIS set to anonymous access to the site. I'm getting this error when turning on anonymous access and disabling integrated security. So response come with 401 error. What I'm doing wriong? – user9061 Jun 23 '12 at 21:08
up vote 4 down vote accepted

Try adding:

_WebClient.Credentials = CredentialCache.DefaultCredentials;
share|improve this answer
Thanks a lot, It works. Do you know if there is a way to know the path to the folder "My Download" in c# ? Thanks again – Thoma Bigueres Jun 4 '12 at 13:54
My downloads is not a SpecialFolder so cannot be accessed like for instance My documents using Environment.SpecialFolder. You can however get to that folder using the KnownFolders of the windows API, see the accepted answer for this question on SO:… – Colin Jun 23 '12 at 22:01
+1 for a great answer, I forgot all about the credentials. – keni Jan 14 '13 at 9:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.