2

iam trying to get all clients from my harvest account... The problem is that the password contains a § sign. I took it away first and it worked but now my boss tells me the password cant be changed for different reasons. ive already tried utf8encoding ect.

public List<Client> GetAllClients()
{
    uri = "https://laios.harvestapp.com/clients";

    using (WebClient webClient = new WebClient())
    {
         webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
         webClient.Headers[HttpRequestHeader.Accept] = "application/json";
         webClient.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword));

         string response = webClient.DownloadString(uri);

         clientsList = JsonConvert.DeserializeObject<List<Wrapper>>(response).Select(c => c.client).ToList();
     }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Robel Haile
  • 309
  • 1
  • 5
  • 18
  • 1
    Can you be specific, What is not working at the moment, ie. what is the behavior that is happening that makes you say that. What happens when you try ASCII.Encoding.GetBytes with `§` and what happens when you execute it with UTF8.Encoding.GetBytes. – Igor Mar 21 '16 at 13:09
  • 1
    A `§` does not exist in the [ASCII encoding](http://www.asciitable.com/) – Hans Kesting Mar 21 '16 at 13:14
  • when the password contains a § i get a error 401 – Robel Haile Mar 21 '16 at 13:22

2 Answers2

2

The § is NOT ASCII, because its code is 167, that is > 127.

You can try with the Encoding.GetEncoding("windows-1252").GetBytes and/or Encoding.GetEncoding("iso-8859-1").GetBytes. To distinguish the two, try a password with the Euro Symbol. The windows-1252 should support it, the iso-8859-1 shouldn't.

See here where they explain that normally the encoding should be iso-8859-1.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

ive already tried utf8encoding ect.

You are being a little general here without any specifics but UTF8 works fine for encoding and decoding a string with this symbol.

var mySTr = "My § sign";
var encoded = System.Text.Encoding.UTF8.GetBytes(mySTr);
var encoding2 = Convert.ToBase64String(encoded);
Console.WriteLine("Base64 encoded: " + encoding2);
var decoded = Convert.FromBase64String(encoding2);
var decodedStr = System.Text.Encoding.UTF8.GetString(decoded);
Console.WriteLine("Decoded again: " + decodedStr);
Debug.Assert(mySTr.Equals(decodedStr, StringComparison.Ordinal));

Edit

comment: when the password contains a § i get a error 401

It is not clear who owns the service that you are communicating with. If this is also under your control then you need to make sure that you use the same encoding to reverse the password. You can't send it in UTF8 and expect the same result when decoded in ASCII. If you do not own it then you need to contact the party who does and ask them their recommendation on handling characters outside the allowed character set of ASCII (if the solution proposed by xanatos does not work).

Further more: If you do own this service then this is NOT a good practice. You are sending an unencrypted unhashed password over the network (maybe even over a public network like the internet)! Security by obscurity is never good! Only a hash of a password should ever be sent. If that can't be done then it should be encrypted but hash is preferred.

Igor
  • 60,821
  • 10
  • 100
  • 175