DownloadFile and Credentials



I am using DownloadFile() on a C# web app to download a file to a
client. When I set the WebClient credentials to the DefaultCredentials,
I get a 401 Unauthorized exception.

WebClient myWC = new WebClient();
myWC.Credentials = CredentialCache.DefaultCredentials;
myWC.DownloadFile(myRemoteUri, myFile);

However, when I set the credentials using a username and password it
works.

myWC.Credentials = new Net.NetworkCredential("username","pwd");

Can someone please explain to me why I cannot use the default
credentials as I do when I'm calling a web service.

MyWS proxy = new MyWS();
proxy.Credentials = CredentialCache.DefaultCredentials;
proxy.CallAMethod();
// this works!

I would expect the DefaultCredentials to work for the WebClient as well
since I have the credentials in cache (from the user login to get to
the page). The web site is configured for Integrated Windows
authentication. The form pages use basic authentication, and the web
services use Integrated Windows (so we can pass the credentials).

Thanks,
Cindy

.



Relevant Pages

  • Re: WebClient and proxy authentication
    ... d> So, if my program uses WebClient to download a file, it will also get ... d> the the un-authenticated error, at which time, the program needs to pop ... d> up the IE/Windows credentials window for the user's credentials. ... CredentialCache myCache = new CredentialCache; ...
    (microsoft.public.dotnet.framework)
  • Re: WebClient + Credentials + HTTP 401 Status Codes
    ... You can do this by setting PreAuthenticate to True on the WebClient. ... but have noticed the WebClient Class does not send the ... credentials I set until it gets an HTTP 401 return code. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: SSO Web Clipping/Page Grafting
    ... You can write your own webpart with the following approach. ... Use SSO to store/retrieve the user's credentials... ... Pass these credentials to a WebClient, and use a WebClient to send a request ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: WebClient + Credentials + HTTP 401 Status Codes
    ... not WebClient. ... but have noticed the WebClient Class does not send ... > credentials I set until it gets an HTTP 401 return code. ... > messing up my program that scans the log files for 401 errors! ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: WebClient + Credentials + HTTP 401 Status Codes
    ... > The WebRequest object has PreAuthenticate, not WebClient. ... but have noticed the WebClient Class does not send ... >> credentials I set until it gets an HTTP 401 return code. ... >> messing up my program that scans the log files for 401 errors! ...
    (microsoft.public.dotnet.framework.aspnet.security)