0

I am trying to connect to a network drive using C# which is password protected. I tried to implement this in a class, which works in principle, because I tested it with a user who has a password without special characters.

The connection is established via the following source code:

Process pro = new Process();
// StorageLetter = A: for example
pro.StartInfo.Arguments = "use " + StorageLetter + @" \\" + IP + @"\c$\ /user:" + User + " " + Password + " /persistent:" + Persistent;
pro.StartInfo.FileName = "net";
pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pro.StartInfo.CreateNoWindow = true;

pro.Start();

pro.WaitForExit();

However, the correct user has a password with special characters (for example: |s/1=;zw}) and therefore establishing the connection will not work.

Here I have already found out that there is a special syntax via the command line. I have also tried to implement this:

private Dictionary<string, string> escapeSequenzToCharacters = new Dictionary<string, string>();

public PasswordModifier()
        {            
            escapeSequenzToCharacters.Add("^", "^");        // = ^
            escapeSequenzToCharacters.Add("\\", "\\\\");    // = \

            escapeSequenzToCharacters.Add("\"", "\"");      // = "
            escapeSequenzToCharacters.Add("%", "%");        // = %
            escapeSequenzToCharacters.Add("&", "^");        // = &
            escapeSequenzToCharacters.Add("<", "^");        // = <
            escapeSequenzToCharacters.Add(">", "^");        // = >
            escapeSequenzToCharacters.Add("|", "^");        // = |
            escapeSequenzToCharacters.Add("'", "^");        // = '
            escapeSequenzToCharacters.Add("`", "^");        // = `
            escapeSequenzToCharacters.Add(",", "^");        // = ,
            escapeSequenzToCharacters.Add(";", "^");        // = ;
            escapeSequenzToCharacters.Add("=", "^");        // = =
            escapeSequenzToCharacters.Add("(", "^");        // = (
            escapeSequenzToCharacters.Add(")", "^");        // = )
            escapeSequenzToCharacters.Add("!", "^^");       // = !
            escapeSequenzToCharacters.Add("[", "\\");       // = [
            escapeSequenzToCharacters.Add("]", "\\");       // = ]
            escapeSequenzToCharacters.Add(".", "\\.");      // = .
            escapeSequenzToCharacters.Add("*", "\\");       // = *
            escapeSequenzToCharacters.Add("?", "\\");       // = ?
        }


public string ModifyForBash(string password)
        {
            foreach (string key in escapeSequenzToCharacters.Keys)
            {
                if (password.Contains(key))
                    password = password.Replace(key, escapeSequenzToCharacters[key] + key);
            }

            return password;
        }

It also returns the correctly edited password for my understanding (|s/1=;zw} = ^|s/1^=^;zw}), but it still does not work when I try to connect to it.

In addition, I have now also tried these methods, but without success:

Now I'm asking how I can do it in C#, that the password with the special characters can be used without problems?

Soeren3003
  • 58
  • 7
  • Try to add double quote around the password. or single. – xdtTransform Apr 28 '20 at 06:40
  • @xdtTransform That didn't work either, unfortunately, neither ' nor " – Soeren3003 Apr 28 '20 at 07:00
  • Have you tried making a direct dll call instead of invoking a command? I would think that may avoid some special character issues. See https://stackoverflow.com/questions/3700871/connect-to-network-drive-with-user-name-and-password – JonasH Apr 28 '20 at 08:23
  • I'm afraid I must amend my statement... This method only works if the drive was already mounted once in the current session. But if I want to mount a drive completely fresh it does not work. – Soeren3003 Apr 29 '20 at 06:14

1 Answers1

0

After some research and tests I found a working solution. With the help of this posting, my application now works without errors.

Soeren3003
  • 58
  • 7