Search code examples
c#linuxwindowsescaping

Post a password with an exclamation mark ! from a Windows server to a Linux server


I've been having a hard time trying to figure out how to post a password that has a exclamation point ! from a Windows server to a Linux server. I only mention linux because linux uses ! for

The following code works as long as there is no exclamation point in the password, as soon as there is an exclamation point in the password, I get a 400 error.

For example; a password of password123 works. a password of password123! fails.

I have tried putting in escape characters into the password to no avail such as I have tried both password123\! and password123!

I think the issue might be windows does not need an escape character before ! but linux does. Not 100% sure on this point but that is what my research has caused me to suspect.

Does anyone know how to solve this?

string Username = "myUserName";
string Password = "password123!";
authenticationString = $"{Username}:{Password}";
base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

var client = new RestClient("https://linuxserver/xml-data/data-producer-listdata");

var request = new RestRequest("", RestSharp.Method.Get);
              request.AddHeader("cache-control", "no-cache");
              request.AddHeader("Authorization", "Basic " + base64EncodedAuthenticationString);
              request.AddHeader("Connection", "keep-alive");
              response = client.Execute(request);

Solution

  • You are transferring your user credentials in a Base64-encoded format via the request header, which appears to be correct to me.

    Therefore, there should not be any issues caused by unescaped characters. Base64 encoding uses only the following characters: [a-zA-Z0-9+/] (and = for padding, if needed).

    The issue you might be facing could relate to the character encoding you are using. Your credentials are first encoded using ASCII.GetBytes(). However, the encoding type you actually need depends on your Linux server's configuration.

    Since I cannot test your functionality myself, I recommend trying UTF-8 encoding, which is more commonly used today.

    base64EncodedAuthenticationString = Convert.ToBase64String(Encoding.UTF8.GetBytes(authenticationString));