Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Authenticate to an IIS and download a file

Posted on 2012-03-26
11
586 Views
Last Modified: 2012-04-06
Hello,

VS2010, C#

  I'm trying to find sample code and IIS configuration information to create a file repository on an IIS server and downloading files in a directory after the user is authenticated in a C# application.

(sorry for some reason its not letting me use the code tags)

WebClient wbcWeb = new WebClient();

Uri webLocation = new Uri("http://filedownload.thedomain.com");
NetworkCredential nwCredendials = new NetworkCredential(@"lUpdater", "passwordcode");
wbcWeb.Credentials = nwCredendials;
wbcWeb.DownloadFile(webLocation, "testfile.exe");

I'm not sure if the code is good/bad but I'm thinking it's probably my web.config.  Does anyone have a sample web.config handy for basic authentication to be used in the fashion.  In addition, how about some sample code in C# for downloading the file?
0
Comment
Question by:sej69
11 Comments
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37769635
web client which not use for your client download.you have to use Reponse.write. or response. transmit file and your file path as argument.

there are too many codes available on google for response.write() but which is gives performance issue use trasmit file.

Regards,
0
 
LVL 3

Expert Comment

by:sfaisalawan
ID: 37769651
Hi, this is the source code of an ASP.Net and C# Application used to upload files to IIS server.  this will save file on web server directory named Data.
 
      protected void UploadFile_Click(object sender, EventArgs e)
        {
                string fileName = FileUpload1.PostedFile.FileName;
                string location = Server.MapPath("~/Data/");
                string ext1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);

                // First check the existing file on the server with same name.
                // If there is a file with same name this will replace it with new one.

                if ((FileUpload1.HasFile) && (File.Exists(fileName)))
                {
                        File.Delete(fileName1);
                        FileUpload2.PostedFile.SaveAs(location + fileName);
                }
      }
0
 

Author Comment

by:sej69
ID: 37771118
I don't think that's quite right...  From what I've read to download a file (not upload from both of your posts) I either need WebClient or HttpWebRequest...  Regardless, I don't want to send a file to my server, I want to retreive a file from the server over the internet to my application running on a client with authentication.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 37782222
I think your code is almost there.  The only thing I'm missing is the file name on the new Uri statement as below:

Uri webLocation = new Uri("http://filedownload.thedomain.com/testfile.exe");

If adding the file name to the Uri does not work, please post the exact error message or unexpected behavior you get when running the code.
0
 

Author Comment

by:sej69
ID: 37786638
I get a file not found (404) if I add the file to it.  Without the file on it I get a (403) access denied.  I believe my issue is with iis possibly.  I'm not 100% sure on how to set up the right authentication.  I'm using basic auth now.  I also tried digest with the same issues.

My current web.config looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <authentication mode="basic" />
    </system.web>
</configuration>
0
 

Author Comment

by:sej69
ID: 37786646
I should also mention that I found this helpful article.  I set up my current code with the sample that's listed here with the addition of the authentication piece.

http://www.geekpedia.com/tutorial179_Creating-a-download-manager-in-Csharp.html
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37786764
What's the url to download the file using IE for example?  And, does IE prompt you for user/password?
0
 

Author Comment

by:sej69
ID: 37791736
Weird thing.  I believe it was working but the .exe file is causing an issue.  I created a file called test.txt in the directory and used the C# app to download it.  So, why is the .exe causing an issue?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37791754
Humm... Anti-virus comes to mind. Try downloading the .exe as a.txt file as a test.
0
 

Accepted Solution

by:
sej69 earned 0 total points
ID: 37792234
I thought that too... However, I don't have AV on this server; it's my test server. The .exe actually isn't an .exe file.  It's a text file names as an .exe that I'm using to test.  Even if I name the file .ex_ or .xxx it doesn't work.  It seems to be tied to the extension somehow like a content type related to an extension.
0
 

Author Closing Comment

by:sej69
ID: 37815441
Ok, it was working all the time.  I've decided to use .zip to compress the file before I send it.  I'll just need to code a decompression routine.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question