By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

DownloadFile and Credentials

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
First, I want to make sure you know that cache is only temporary (You
shouldn't rely on it, right code to check for it, it is simply to speed
things up). Second, it looks to me like you are writing code more
complicated than necessary. Here is a much simpler way to have a client
download a file, and here is the website where I learned how to do it:

Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDownload.Click

Response.ClearContent()

Response.ContentType = "text/plain"

Response.AddHeader("content-disposition",
"attachment;filename=download.txt")

'Response.WriteFile(Server.MapPath("download.txt") )

Response.Write("This is a test download text file" & ControlChars.NewLine)

Response.Write(Date.Now.ToLongDateString() & " " &
Date.Now.ToLongTimeString())

Response.End()

End Sub
My example lets the client download a text file that contains the following
text:

This is a test download text file
<date> <time>

If you want to have the client download another file type, replace
"text/plain" with the MIME type of your file and use the WriteFile method
instead of the Write method.

http://www.dotnetspider.com/technology/kbpages/553.aspx

Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

<ci**********@emersonProcess.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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

Nov 19 '05 #2

P: n/a
Thanks for your comments, Nathan. I need to use WebClient instead of
HttpRequest because the download is not necessarily in response to a
client request.

I did figure out the problem, though. It turns out that the directory
of the file to download (myRemoteUri in my sample code) was set for
Basic authentication. You cannot pass default credentials with Basic
authentication, but you can with NTLM. I actually knew this, but the
pages were using NTLM so I figured it would be ok. It works fine for
passing the credentials to webservices. But I guess because I am
accessing the directory to get the file that the directory must need it
too. After I changed the virtual directory authentication setting to
NTLM, it worked fine.

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.