-
-
Save pcolombo/dbbea020618c521a2bd5 to your computer and use it in GitHub Desktop.
Using JWT to generate a livefyre collection meta jwt token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated on 2/24/15 with correct meta.Add