Our team has created a boiler plate example for acquiring the JWT utilizing C# language:
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using System.Security.Claims;
using System.Net.Http;
using System.Text;
using System.IO;
namespace CreateJWT
{
public static class GenerateJWT
{
private const string CLIENT_KEY = "<CLIENT_KEY>";
private const string CLIENT_SECRET = "<CLIENT_SECRET >";
private const string AUD = "https://<SERVER_NAME>.iformbuilder.com/exzact/api/oauth/token";
public static void Main(string[] args)
{
GenerateJWT.generateJwtToken1();
}
public static void generateJwtToken1()
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(CLIENT_SECRET);
DateTime epoch = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc);
long ms = (long) (DateTime.UtcNow - epoch).TotalMilliseconds;
long ms10 = ms * 60000 * 10;
var claims = new[] {
new Claim("iss", CLIENT_KEY),
new Claim("aud", AUD),
new Claim("iat", ms.ToString()),
new Claim("exp", ms10.ToString())
};
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.UtcNow.AddMinutes(10),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
//Console.WriteLine("Token: " + tokenHandler.WriteToken(token));
GenerateJWT.postCall(tokenHandler.WriteToken(token));
}
public static void postCall(string token)
{
var values = new Dictionary<string, string>
{
{ "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
{ "assertion", token }
};
var content = new FormUrlEncodedContent(values);
try {
var handler = new HttpClientHandler();
var client = new HttpClient(handler) {
BaseAddress = new Uri(AUD)
};
var webRequest = new HttpRequestMessage(HttpMethod.Post, AUD) {
Content = content
};
var response = client.Send(webRequest);
var reader = new StreamReader(response.Content.ReadAsStream());
var responseBody = reader.ReadToEnd();
Console.Out.WriteLine("Token: " + responseBody);
} catch (Exception ex) {
Console.Out.WriteLine("Exception: " + ex);
}
}
}
}
Feb. 15, 2017:
Our team has created a boiler plate example for acquiring the JWT in VB:
Comments
0 comments
Please sign in to leave a comment.