Solved

C# Authenticate to an IIS and download a file

Posted on 2012-03-26
11
575 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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