Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string Timestamp()
- {
- long ticks = DateTime.UtcNow.Ticks -
- DateTime.Parse("01/01/1970 00:00:00").Ticks;
- ticks /= 10000000;
- return ticks.ToString();
- }
- public string Hash(string input, string key)
- {
- var encoding = new System.Text.ASCIIEncoding();
- byte[] keyByte = encoding.GetBytes(key);
- HMACSHA1 myhmacsha1 = new HMACSHA1(keyByte);
- byte[] byteArray = Encoding.ASCII.GetBytes(input);
- MemoryStream stream = new MemoryStream(byteArray);
- byte[] hashValue = myhmacsha1.ComputeHash(stream);
- return string.Join("", Array.ConvertAll(hashValue, b => b.ToString("x2")));
- }
- public string GetToken()
- {
- if (HttpContext.Current == null || String.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Cache["QuickBloxToken"])))
- {
- string url = "https://api.quickblox.com"; //ConfigurationManager.AppSettings["ChatUrl"].ToString();
- HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url + "/session.xml");
- httpWReq.UserAgent = ".NET Framework Test Client";
- string application_id = System.Configuration.ConfigurationManager.AppSettings["QuickApplication_id"].ToString();
- string auth_key = System.Configuration.ConfigurationManager.AppSettings["QuickAuth_key"].ToString();
- string timestamp = Timestamp();
- string auth_secret = System.Configuration.ConfigurationManager.AppSettings["QuickAuth_secret"].ToString();
- ASCIIEncoding encoding = new ASCIIEncoding();
- string postData = "application_id=" + application_id;
- postData += "&auth_key=" + auth_key;
- Random rand = new Random();
- postData += "&nonce=" + rand.Next(1000, 9999);
- postData += "×tamp=" + timestamp;
- string signature = Hash(postData, auth_secret);
- postData += "&signature=" + signature;
- byte[] data = encoding.GetBytes(postData);
- httpWReq.Method = "POST";
- httpWReq.ContentLength = data.Length;
- httpWReq.Headers["QuickBlox-REST-API-Version"] = "0.1.0";
- using (Stream stream = httpWReq.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
- string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(responseString);
- var nodes = xmlDoc.SelectNodes("session");
- string token = nodes[0].SelectSingleNode("token").InnerText;
- if (HttpContext.Current != null)
- HttpContext.Current.Cache.Add("QuickBloxToken", token, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 70, 0), System.Web.Caching.CacheItemPriority.Default, null);
- return token;
- }
- else
- return Convert.ToString(HttpContext.Current.Cache["QuickBloxToken"]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement