Asp.net Access To Network Share

I have an ASP.NET app which needs to save files to a network share (Samba).

The share requires a username and password to connect.

I have mapped a persistent drive to the share and provided the login credentials whilst logged in to the server as DOMAINWEBUSER.

I have changed the virtual directory which hosts my app to use the DOMAINWEBUSER account instead of the IWAM account.

However the user still cannot see the mapped drive.

What am I missing out?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Did you try mapping the drive in code? Here is a class for doing just that…

public static class NetworkDrives
    {
        public static bool  MapDrive(string DriveLetter, string Path, string Username, string Password)
        {

            bool ReturnValue = false;

            if(System.IO.Directory.Exists(DriveLetter + ":\"))
            {
                DisconnectDrive(DriveLetter);
            }
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": " + Path + " " + Password + " /user:" + Username;
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }
        public static bool DisconnectDrive(string DriveLetter)
        {
            bool ReturnValue = false;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": /DELETE";
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }

    }

Method 2

It is best to use the UNC if you can as the mapped drives generally are linked to the interactive user and the virtual directory is probably connecting with a service or netwrok login type.

Another possible fix is described in this KB articleError occurs when you configure IIS to use a Samba network share as its root. Excerpted below.

Important These steps may increase
your security risk. These steps may
also make the computer or the network
more vulnerable to attack by malicious
users or by malicious software such as
viruses. We recommend the process that
this article describes to enable
programs to operate as they are
designed to or to implement specific
program capabilities. Before you make
these changes, we recommend that you
evaluate the risks that are associated
with implementing this process in your
particular environment. If you decide
to implement this process, take any
appropriate additional steps to help
protect the system. We recommend that
you use this process only if you
really require this process.

Warning This method involves a
security risk because the user who
created the mapping must remain logged
on to the local console. Therefore,
the only security is by locking the
computer. To work around this problem,
do the following:

  1. Map a drive letter to servernameiisroot using “root” and
    “password.”
  2. In the Samba virtual directory, change the home directory from Share
    on Another Computer to Local
    Directory, and then specify the drive
    letter that you mapped in step 1.
  3. Restart the Web site, and then test it by browsing.

Method 3

It’s been a while (6 or 8 years) since I’ve done something like this, but as I recall, I also had to run the APP Pool as the domain user that had access to the network share in order for IIS to be able to serve up files from it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x