DownloadFile and Credentials

C

cindy.fisher

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
 
N

Nathan Sokalski

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!
 
C

cindy.fisher

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top