title: Binance API HMAC SHA256 Signature error Using C#
body: <p>I try to connect to the <code>Binance Api</code> Using <code>C#</code> And <code>RestSharp</code> lib. Based on <a href="https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#endpoint-security-type" rel="nofollow noreferrer">documents</a> I should try something like this:  </p>
  
  <blockquote>
    <p>queryString: symbol=LTCBTC&amp;side=BUY&amp;type=LIMIT&amp;timeInForce=GTC&amp;quantity=1&amp;price=0.1&amp;recvWindow=5000&amp;timestamp=1499827319559</p>
    
    <p>HMAC SHA256 signature:</p>
    
    <p>[linux]$ echo -n
    "symbol=LTCBTC&amp;side=BUY&amp;type=LIMIT&amp;timeInForce=GTC&amp;quantity=1&amp;price=0.1&amp;recvWindow=5000&amp;timestamp=1499827319559"
    | openssl dgst -sha256 -hmac
    "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
    (stdin)=
    c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71 curl
    command:</p>
    
    <p>(HMAC SHA256) [linux]$ curl -H "X-MBX-APIKEY:
    vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A" -X
    POST
    <a href="https://api.binance.com/api/v3/order?symbol=LTCBTC&amp;side=BUY&amp;type=LIMIT&amp;timeInForce=GTC&amp;quantity=1&amp;price=0.1&amp;recvWindow=5000&amp;timestamp=1499827319559&amp;signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71" rel="nofollow noreferrer">https://api.binance.com/api/v3/order?symbol=LTCBTC&amp;side=BUY&amp;type=LIMIT&amp;timeInForce=GTC&amp;quantity=1&amp;price=0.1&amp;recvWindow=5000&amp;timestamp=1499827319559&amp;signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71</a></p>
  </blockquote>
  
  <p>I try many ways and many stack questions but I found solutions for other languages not c#. 
  this i I done based on: <a href="https://stackoverflow.com/questions/12185122/calculating-hmacsha256-using-c-sharp-to-match-payment-provider-example">this</a> and <a href="https://stackoverflow.com/questions/50304411/binance-api-hmac-signature">this</a></p>
  
  <pre><code>public string GetAccountInfo()
      {
          long timeStamp = GetTimestamp();
          var request = new RestRequest("api/v3/account");
          request.AddHeader("X-MBX-APIKEY", ApiKey);
          request.AddParameter("timestamp", timeStamp, ParameterType.QueryString);
          request.AddParameter("recvWindow", RecvWindow, ParameterType.QueryString);
          request.AddParameter("signature", GetSignature(request.Parameters), ParameterType.QueryString);
          return Client.Get(request).Content;
      }
  private long GetTimestamp()
      {
          return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
      }
  
      private string GetSignature(List&lt;Parameter&gt; parameters)
      {
          var signature = "";
          if(parameters.Count &gt; 0)
          {
              foreach (var item in parameters)
              {
                  if(item.Name != "X-MBX-APIKEY")
                      signature += $"{item.Name}={item.Value}&amp;";
              }
              signature = signature.Substring(0, signature.Length - 2);
          }
          byte[] keyBytes = Encoding.Unicode.GetBytes(ApiSecret);
          byte[] valueBytes = Encoding.Unicode.GetBytes(signature);
          return HashEncode(HashHMAC(keyBytes, valueBytes));
      }
  
      private static byte[] HashHMAC(byte[] key, byte[] message)
      {
          var hash = new HMACSHA256(key);
          return hash.ComputeHash(message);
      }
  
      private static string HashEncode(byte[] hash)
      {
          return BitConverter.ToString(hash).Replace("-", "").ToLower();
      }
  </code></pre>
  
  <p>but I got this error: Signature for this request is not valid. How can I fix it? Thank you.</p>