public ContextUtility(HttpRequest request)
        {
            ContextDetails = new SpContext { ServerUrl = request.Url.Authority, HostWebUrl = HttpContext.Current.Request["SPHostUrl"], AppWebUrl = HttpContext.Current.Request["SPAppWebUrl"], ContextTokenString = TokenHelper.GetContextTokenFromRequest(request) };

            if (ContextToken == null)
            {
                try
                {
                    ContextToken = TokenHelper.ReadAndValidateContextToken(ContextDetails.ContextTokenString, ContextDetails.ServerUrl);
                }
                catch (Exception)
                {
                    ContextToken = null;
                }
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            HttpCookie cookie = new HttpCookie("SPContext", serializer.Serialize(ContextDetails));
            cookie.Expires = DateTime.Now.AddHours(12);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
 public ContextUtility(SpContext context)
 {
     ContextDetails = context;
     try
     {
         ContextToken = TokenHelper.ReadAndValidateContextToken(ContextDetails.ContextTokenString, ContextDetails.ServerUrl);
     }
     catch (Exception)
     {
         ContextToken = null;
     }
 }