Solved

C# Authenticate to an IIS and download a file

Posted on 2012-03-26
11
564 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting database in Xamarin? 6 47
Need to pass a connection through class/Method 1 39
Problem to error 4 43
Angular JS Route 3 44
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 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

19 Experts available now in Live!

Get 1:1 Help Now