Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Authenticate to an IIS and download a file

Posted on 2012-03-26
11
Medium Priority
?
667 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
[X]
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
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
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:…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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