Skip to content

Instantly share code, notes, and snippets.

@pcolombo
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcolombo/dbbea020618c521a2bd5 to your computer and use it in GitHub Desktop.
Save pcolombo/dbbea020618c521a2bd5 to your computer and use it in GitHub Desktop.
Using JWT to generate a livefyre collection meta jwt token
using Newtonsoft.Json;
using PROJECT.Web.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Dynamic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace PROJECT.Web.Helpers
{
public class LivefyreHelper
{
public static String GetNetwork()
{
return ConfigurationManager.AppSettings["livefyreNetwork"];
}
public static String GetSiteId()
{
return ConfigurationManager.AppSettings["livefyreSiteId"];
}
public static String GetSiteSecret()
{
return ConfigurationManager.AppSettings["livefyreSecretKey"];
}
public static String GetCollectionToken(String ID, MetaInfo metaInfo)
{
string siteSecret = GetSiteSecret();
var meta = new Dictionary<string, object>() {
{"title", metaInfo.Title},
{"url", metaInfo.Url},
{"tags", ""},
{"type", "livecomments"}
};
var metaStr = JsonConvert.SerializeObject(meta);
byte[] hash;
using (var md5 = MD5.Create())
{
hash = md5.ComputeHash(Encoding.UTF8.GetBytes(metaStr));
}
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < hash.Length; i++)
{
sBuilder.Append(hash[i].ToString("x2"));
}
meta.Add("articleId", ID);
string token = JWT.JsonWebToken.Encode(meta, siteSecret, JWT.JwtHashAlgorithm.HS256);
return token;
}
}
}
@pcolombo
Copy link
Author

Updated on 2/24/15 with correct meta.Add

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment