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

  • @Dimiikou you helped allot! I didn't exactly do your suggestion but I took a closer look at the encoding.

    I changed the encoding from

    base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));
    

    to

    base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(authenticationString));
    

    and it worked!!!