Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. public string Timestamp()
  2. {
  3. long ticks = DateTime.UtcNow.Ticks -
  4. DateTime.Parse("01/01/1970 00:00:00").Ticks;
  5. ticks /= 10000000;
  6. return ticks.ToString();
  7. }
  8. public string Hash(string input, string key)
  9. {
  10. var encoding = new System.Text.ASCIIEncoding();
  11. byte[] keyByte = encoding.GetBytes(key);
  12. HMACSHA1 myhmacsha1 = new HMACSHA1(keyByte);
  13. byte[] byteArray = Encoding.ASCII.GetBytes(input);
  14. MemoryStream stream = new MemoryStream(byteArray);
  15. byte[] hashValue = myhmacsha1.ComputeHash(stream);
  16. return string.Join("", Array.ConvertAll(hashValue, b => b.ToString("x2")));
  17. }
  18. public string GetToken()
  19. {
  20. if (HttpContext.Current == null || String.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Cache["QuickBloxToken"])))
  21. {
  22. string url = "https://api.quickblox.com"; //ConfigurationManager.AppSettings["ChatUrl"].ToString();
  23. HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url + "/session.xml");
  24.  
  25. httpWReq.UserAgent = ".NET Framework Test Client";
  26.  
  27. string application_id = System.Configuration.ConfigurationManager.AppSettings["QuickApplication_id"].ToString();
  28. string auth_key = System.Configuration.ConfigurationManager.AppSettings["QuickAuth_key"].ToString();
  29. string timestamp = Timestamp();
  30. string auth_secret = System.Configuration.ConfigurationManager.AppSettings["QuickAuth_secret"].ToString();
  31.  
  32. ASCIIEncoding encoding = new ASCIIEncoding();
  33. string postData = "application_id=" + application_id;
  34. postData += "&auth_key=" + auth_key;
  35.  
  36.  
  37. Random rand = new Random();
  38.  
  39. postData += "&nonce=" + rand.Next(1000, 9999);
  40. postData += "&timestamp=" + timestamp;
  41. string signature = Hash(postData, auth_secret);
  42. postData += "&signature=" + signature;
  43. byte[] data = encoding.GetBytes(postData);
  44.  
  45. httpWReq.Method = "POST";
  46.  
  47. httpWReq.ContentLength = data.Length;
  48. httpWReq.Headers["QuickBlox-REST-API-Version"] = "0.1.0";
  49.  
  50. using (Stream stream = httpWReq.GetRequestStream())
  51. {
  52. stream.Write(data, 0, data.Length);
  53. }
  54.  
  55. HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
  56. string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  57.  
  58. XmlDocument xmlDoc = new XmlDocument();
  59. xmlDoc.LoadXml(responseString);
  60.  
  61. var nodes = xmlDoc.SelectNodes("session");
  62. string token = nodes[0].SelectSingleNode("token").InnerText;
  63.  
  64. if (HttpContext.Current != null)
  65. HttpContext.Current.Cache.Add("QuickBloxToken", token, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 70, 0), System.Web.Caching.CacheItemPriority.Default, null);
  66. return token;
  67. }
  68. else
  69. return Convert.ToString(HttpContext.Current.Cache["QuickBloxToken"]);
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement