Hello, i havethis exemple :
And am trying to make it work with my WPF.
Here is my code : The key is the real key, (read only actions) so you can try this.
i got 401 error, i cannot be autentified. I tryed a lots of think, and am now lost and without more ideas, please can someone help me?
public class JWT1
{
private const string tokenId = "419506";
private const string key = "/V2qzwXZ6+inAQ/ygqTUMq5yEYOZBOSVzOEK6wjGJuh/YpaP/y7a1ge8s03ycEWVcAgaQt1ghmNRg5Izk3nHNw==";
public static void Test()
{
GetRequest("/orders?product_id=1");
}
private static void GetRequest(string getString)
{
string encoded = Encode(getString);
Debug.WriteLine("token : \n" + encoded);
WebRequest request = WebRequest.Create("https://api.quoine.com"+ getString);
request.Method = "GET";
request.Timeout = 12000;
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
request.ContentType = "application/json";
request.Headers.Add("X-Quoine-API-Version", "2");
request.Headers.Add("X-Quoine-Auth", encoded);
WebResponse response = null;
try
{
response = request.GetResponse();
}
catch (Exception)
{
throw;
}
finally
{
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
Debug.WriteLine("encoded : " + encoded);
//Debug.WriteLine("StatuDesc : " + response.StatusDescription);
//Debug.WriteLine("Statucode : " + response.StatusCode);
//Debug.WriteLine("Charac : " + response.CharacterSet);
Debug.WriteLine("Stream : " + reader.ReadToEnd());
response.Close();
}
}
private static string Encode(string request)
{
// Create Security key using private key above:
// not that latest version of JWT using Microsoft namespace instead of System
//SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Convert.FromBase64String(key));
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
// Also note that securityKey length should be >256b
// so you have to make sure that your private key has a proper length
SigningCredentials credentials = new SigningCredentials
(securityKey, SecurityAlgorithms.HmacSha256);
// Finally create a Token
JwtHeader header = new JwtHeader(credentials);
//Some PayLoad that contain information about the customer
JwtPayload payload = new JwtPayload
{
{ "path ", request},
{ "nonce", DateTime.Now.Ticks},
{"token_id",tokenId }
};
//IdentityModelEventSource.ShowPII = true;
//Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]', SecurityKey: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'
JwtSecurityToken secToken = new JwtSecurityToken(header, payload);
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
// Token to String so you can use it in your client
return handler.WriteToken(secToken);
}
}