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?