473,786 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 DefaultCredenti als,
I get a 401 Unauthorized exception.

WebClient myWC = new WebClient();
myWC.Credential s = CredentialCache .DefaultCredent ials;
myWC.DownloadFi le(myRemoteUri, myFile);

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

myWC.Credential s = new Net.NetworkCred ential("usernam e","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.Credentia ls = CredentialCache .DefaultCredent ials;
proxy.CallAMeth od();
// this works!

I would expect the DefaultCredenti als 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
2 4182
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_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnDownload.Cli ck

Response.ClearC ontent()

Response.Conten tType = "text/plain"

Response.AddHea der("content-disposition",
"attachment;fil ename=download. txt")

'Response.Write File(Server.Map Path("download. txt"))

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

Response.Write( Date.Now.ToLong DateString() & " " &
Date.Now.ToLong TimeString())

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********@hotm ail.com
http://www.nathansokalski.com/

<ci**********@e mersonProcess.c om> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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 DefaultCredenti als,
I get a 401 Unauthorized exception.

WebClient myWC = new WebClient();
myWC.Credential s = CredentialCache .DefaultCredent ials;
myWC.DownloadFi le(myRemoteUri, myFile);

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

myWC.Credential s = new Net.NetworkCred ential("usernam e","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.Credentia ls = CredentialCache .DefaultCredent ials;
proxy.CallAMeth od();
// this works!

I would expect the DefaultCredenti als 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4706
by: mk | last post by:
Hi, I am trying to download a file from a remote webserver. I create the following: string username="remoteusername"; string password="remotepassword"; string URI="http://remote.target.server/getdocument.asp?ID=sometest.pdf"; NetworkCredential nc = new NetworkCredential(username, password); WebRequest req = WebRequest.Create(URI);
6
13304
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileID.ToString() + ".pdf");
6
11088
by: Chris | last post by:
Hi, I try to download a file using the following : WebClient client = new WebClient(); client.Credentials = new NetworkCredential("Administrator", "test","MYPC"); client.DownloadFile("http://localhost/afile.txt", "c:\\test.txt"); but I get an error :
8
9025
by: DEWright_CA | last post by:
Why does WebClient.DownloadFile loose my completed path? Ok, I have a function in my app then when my button is clicked it checks to see if the files come from a local drive or a http address. The portion of the code that pulls from the drive works great, but when the http address is active it changes the originating path to my C:\Windows\System32 folder instead of the proper path. I keep the path as a field on my form so I am certain...
3
1870
by: IntraRELY | last post by:
I have a loop that loops through this sub. However, I recieve an WebException error in my try statement. "An exception occurred during a WebClient request." This message doesnt happen when I am only looping throught the sub once, however when I process multiple records, I recieve this error for every record after the first one. And the images that is downloaded does show up for all records processed. So everything works there is just an...
6
1318
by: Morten Snedker | last post by:
I have this Class: Imports System.Net Public Class DownloadFile Private _webClt As WebClient Private _url As String Private _file As String Sub New(ByVal urlSource As String, ByVal fileDestination As String)
4
14288
by: gavkel | last post by:
Can anyone get this to work? All Im getting is the login html being downloaded. Would appreciate any help - my heads hurts Thanks Gav
2
1619
by: Bill Sempf | last post by:
Hi, all. I have a networking question, really. I have this line of code: my.computer.network.downloadfile("\\1.2.3.4\c$\file.txt", "c:\ifle.txt", "user", "pass") When I run it, I get an access denied error from the framework. If I go to the run menu on my workstation, and type: \\1.2.3.4\c$\
8
9528
by: =?Utf-8?B?UnVpIE9saXZlaXJh?= | last post by:
WebClient.DownloadFile I am using the WebClient.DownloadFile function to download a file from server to a local client. When I execute the below code, file is created in server and not in client. What am I doing wrong?
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.