Solved

C# Authenticate to an IIS and download a file

Posted on 2012-03-26
11
556 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now