Upload file via ASP.NET to Network Share Drive - USERNAME AND PASSWORD REQ

Posted on 2006-04-21
Last Modified: 2011-10-03
What I am trying to do is upload a file from the local computer onto a network share drive.
I am not quite sure how to add a Username/Password to my code below... I've taken the code from here:

Can anyone help?

Thanks in advance....

Select File to Upload: <input id="File1" type="file" name="File1" runat="server">
<p>Save as filename (no path): <input id="Text1" type="text" name="Text1" runat="server">
<p><span id="Span1" style="FONT: 8pt verdana" runat="server"></span>
<p><input id="btnUpload" type="button" value="Upload" name="btnUpload" runat="server">

Private Sub btnUpload_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.ServerClick
        If Text1.Value = "" Then
            Span1.InnerHtml = "Error: you must enter a file name"
        End If

        If Not (File1.PostedFile Is Nothing) Then
                File1.PostedFile.SaveAs(("\\{server}\{Directory}\" & Text1.Value))
                Span1.InnerHtml = "File uploaded successfully to the <b>server as " & _
                                  Text1.Value & "</b> on the Web server"
            Catch exc As Exception
                Span1.InnerHtml = "Error saving file to the <b>Server as " & _
                                  Text1.Value & "</b><br>" & exc.ToString()
            End Try
        End If

    End Sub
Question by:maidstone
    LVL 24

    Expert Comment

    by:DBAduck - Ben Miller
    There are a couple of ways to do this:

    1) is to have the user that is in the Application Pool identity be a user that can auto pass through in authentication because the same user exists on the network machine you are connecting to.  
    2) You can Create a Windows Identity principal and use the username and password and then impersonate and save. This is the more intensive one because there are ramifications that you need to make sure that you protect yourself from and this is also the slowest because it involves authentication of each request.

    Does this make sense?

    Author Comment

    I dont think I quite understand either...
    LVL 24

    Expert Comment

    by:DBAduck - Ben Miller
    They both have to do with a Windows Web Server.

    So if you are wanting to put a file somewhere where the servers are connected, the only way to save a file somewhere is to have a Username/Password that has access to the place.

    So in Web Server land there are 2 places.  1 is the Application Pool Identity that is set in Internet Services Manager on the web server, and the other one is to authenticate via code and use the Username and Password (to create a Windows Principal) and then save the file.

    The first one is more common because the Windows Principal concept for something like this is way overkill and more complex.

    Does that help?

    Author Comment

    What you are saying makes no sence to me....
    What I did was just put a entry into my Web.Config file....

    <identity impersonate="true" userName="{Username}"
    password="{Password}" />

    I don't think thats the solution you were trying to get accross to me, so I went a different direction.
    LVL 24

    Expert Comment

    by:DBAduck - Ben Miller
    Whatever the recommendation or protocol is works for me.


    Accepted Solution

    Closed, 500 points refunded.
    The Experts Exchange
    Community Support Moderator of all Ages

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    733 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

    18 Experts available now in Live!

    Get 1:1 Help Now