Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

1 Answer

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: stackoverflow.com/questions/7672774/… –  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
add comment

Your Answer

 
discard

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.