private void CheckShoppingCartCookie()
    {
        bool     flagadmin = false;
        bool     userApi   = objKenticoService.CheckLoginUser("");
        UserInfo userdata  = CMS.Membership.UserInfoProvider.GetUserInfo(MembershipContext.AuthenticatedUser.UserName);

        DataTable dt = UserInfoProvider.GetUserRoles(userdata);

        if (dt.Rows.Count > 0 && dt != null)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["RoleName"].ToString().ToLower().Contains("administrator"))
                {
                    flagadmin = true;
                }
            }
        }
        if (flagadmin)
        {
            return;
        }

        HttpCookie ck = Request.Cookies[PersonifyControlBase.PersonifyShoppingCartGuidCookieName];

        if (userApi)
        {
            if (ck != null)
            {
                if (!string.IsNullOrEmpty(ck.Value))
                {
                    if (HttpContext.Current.Session["userClass"] != null)
                    {
                        userinfo ui = (userinfo)HttpContext.Current.Session["userClass"];


                        //if (ud.CustomProperties[remoteId] != null)

                        string masterCustomerId = ui.ID.ToString();
                        if (!string.IsNullOrEmpty(masterCustomerId))
                        {
                            TransferShoppingCartItems(ck.Value, masterCustomerId);
                            ClearShoppingCartCookie();
                        }
                    }
                }
            }
        }
        else
        {
            if (ck == null || string.IsNullOrEmpty(ck.Value))
            {
                CreateShoppingCartCookie();
            }
        }
    }
    private void CheckSsoToken()
    {
        bool loggedin = objKenticoService.CheckLoginUser("");

        if (!loggedin)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ct"]))
            {
                var customerToken = Request.QueryString["ct"];
                Session["cuctomerReturnToken"] = customerToken;
                var decryptedToken = DecryptCustomerToken(customerToken);
                Session["OpenLoginBox"] = true;

                var finalToken = "";
                if (decryptedToken != "")
                {
                    finalToken = ValidateCustomerToken(decryptedToken);
                }
                // in case of corrupted decrypted token, until success, retry authentication x times as specified in web config to get new customer token
                if (finalToken == null && Convert.ToInt32(Session["retryAuth"]) < Convert.ToInt32(ConfigurationManager.AppSettings["PersonifyRetryCount"]))
                {
                    EventLogProvider.LogInformation("Personify corrupt decryption string", "Personify", "customer token: " + customerToken + "decrypted token: " + decryptedToken);
                    RetryAuth();
                }
                Session.Remove("uname");
                Session.Remove("pwd");
                var    customerIdentifier = "";
                string emailaddress       = null;
                string userName           = null;

                if (finalToken != "")
                {
                    customerIdentifier          = ValidateUser(finalToken, ref emailaddress, ref userName);
                    Session["PersonifyToken"]   = finalToken;
                    Session["redirecttomember"] = true;
                    Session["retryAuth"]        = Convert.ToInt32(ConfigurationManager.AppSettings["PersonifyRetryCount"]);
                    LoginUsertokentico.WriteError("token for " + userName, finalToken);
                }


                var eUserData = AuthenticateCustomer(customerIdentifier, emailaddress, userName);

                if (eUserData == null)
                {
                    return;
                }

                //use master customer id as the default password
                if (customerIdentifier != null)
                {
                    var defaultPassword = customerIdentifier.Split('|')[0];
                    if (Session["PersonifyToken"] == null && !string.IsNullOrEmpty(finalToken))
                    {
                        Session["PersonifyToken"] = finalToken;
                    }
                }

                if (_wsSso != null)
                {
                    _wsSso.Dispose();
                }

                var hasToken = Request.Url.AbsoluteUri.IndexOf("ct=", StringComparison.Ordinal) > 0;
                Session["redirecttomember"] = true;

                Response.Redirect(
                    hasToken
                        ? Request.Url.AbsoluteUri.Substring(0,
                                                            Request.Url.AbsoluteUri.IndexOf("ct=", StringComparison.Ordinal) - 1)
                        : Request.Url.AbsoluteUri, true);
            }
            else
            {
                if (Session["LoginAttempted"] != null)
                {
                    bool loginAttempted;
                    bool.TryParse(Session["LoginAttempted"].ToString(), out loginAttempted);
                    if (loginAttempted)
                    {
                        Session["OpenLoginBox"] = true;
                        Session.Remove("LoginAttempted");
                    }
                }
            }
        }
        else
        {
            bool flagpersonifyMember = false;
            //if logged in as admin user  keep the userlogged in
            /// If personify token is expired logout user
            ///
            if (CMS.Membership.AuthenticationHelper.IsAuthenticated())
            {
                UserInfo userdata =
                    CMS.Membership.UserInfoProvider.GetUserInfo(MembershipContext.AuthenticatedUser.UserName);

                DataTable dt = UserInfoProvider.GetUserRoles(userdata);

                if (dt.Rows.Count > 0 && dt != null)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["RoleName"].ToString().ToLower().Contains("peronifyuser"))
                        {
                            flagpersonifyMember = true;
                        }
                    }
                }

                ///if personify key is null or empty
                ///
                if (flagpersonifyMember)
                {
                    string tokenReturn = null;
                    if (Session[PersonifySessionKey] != null)
                    {
                        tokenReturn = this.ValidateCustomerToken(Session[PersonifySessionKey].ToString());
                    }

                    if ((Session[PersonifySessionKey] == null) || tokenReturn == null)
                    {
                        /* Response.Redirect(
                         * Request.ServerVariables["PATH_INFO"] + "?action=logout&returnurl=" +
                         * Server.UrlEncode(Request.Url.AbsoluteUri), true);*/

                        //logout user
                        if (Session["userClass"] != null && Session["userClass"] != "")
                        {
                            userinfo ui = (userinfo)Session["userClass"];
                            objKenticoService.Logout(ui.username);
                            System.Web.Security.FormsAuthentication.SignOut();
                            HttpContext.Current.Response.Cookies["ASPXFORMSAUTH"].Expires = DateTime.Now.AddYears(-1);


                            Response.Redirect("/logout.aspx");
                        }
                    }
                }
            }
        }
    }
    private void CheckSsoToken()
    {
        //  var userApi = new UserAPI();
        bool loggedin = objKenticoService.CheckLoginUser("");

        if (!loggedin)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ct"]))
            {
                lblMessage.Text = Request.QueryString["ct"];
                //  Ektron.Cms.Instrumentation.Log.WriteError(Request.QueryString["ct"]);
                var customerToken = Request.QueryString["ct"];
                ////
                Session["cuctomerReturnToken"] = customerToken;
                var decryptedToken = DecryptCustomerToken(customerToken);
                // Ektron.Cms.Instrumentation.Log.WriteError(decryptedToken);
                Session["OpenLoginBox"] = true;

                var finalToken = "";
                if (decryptedToken != "")
                {
                    finalToken = ValidateCustomerToken(decryptedToken);
                }

                var    customerIdentifier = "";
                string emailaddress       = null;
                string userName           = null;

                if (finalToken != "")
                {
                    customerIdentifier          = ValidateUser(finalToken, ref emailaddress, ref userName);
                    Session["PersonifyToken"]   = finalToken;
                    Session["redirecttomember"] = true;

                    LoginUsertokentico.WriteError("token for " + userName, finalToken);
                    if (string.IsNullOrEmpty(customerIdentifier))
                    {
                        //Response.Redirect("http://rapstst75.ebiz.uapps.net/Home/RegisterCustomer.aspx?Email=" +
                        //                  emailaddress + "&returnurl=" +
                        //                  "http://ek9-raps.syscomservices.com/sso_test.aspx");
                    }
                }


                var eUserData = AuthenticateCustomer(customerIdentifier, emailaddress, userName);

                if (eUserData == null)
                {
                    return;
                }

                //use master customer id as the default password
                if (customerIdentifier != null)
                {
                    var defaultPassword = customerIdentifier.Split('|')[0];

                    // var result = MyProvider.ValidateUser(eUserData.Username, defaultPassword);

                    if (Session["PersonifyToken"] == null && !string.IsNullOrEmpty(finalToken))
                    {
                        Session["PersonifyToken"] = finalToken;
                    }
                }

                if (_wsSso != null)
                {
                    _wsSso.Dispose();
                }

                var hasToken = Request.Url.AbsoluteUri.IndexOf("ct=", StringComparison.Ordinal) > 0;
                Session["redirecttomember"] = true;

                Response.Redirect(
                    hasToken
                        ? Request.Url.AbsoluteUri.Substring(0,
                                                            Request.Url.AbsoluteUri.IndexOf("ct=", StringComparison.Ordinal) - 1)
                        : Request.Url.AbsoluteUri, true);
            }
            else
            {
                if (Session["LoginAttempted"] != null)
                {
                    bool loginAttempted;
                    bool.TryParse(Session["LoginAttempted"].ToString(), out loginAttempted);
                    if (loginAttempted)
                    {
                        Session["OpenLoginBox"] = true;
                        Session.Remove("LoginAttempted");
                    }
                }
            }
        }
        else
        {
            bool flagpersonifyMember = false;
            //if logged in as admin user  keep the userlogged in
            /// If personify token is expired logout user
            ///
            if (CMS.Membership.AuthenticationHelper.IsAuthenticated())
            {
                UserInfo userdata =
                    CMS.Membership.UserInfoProvider.GetUserInfo(MembershipContext.AuthenticatedUser.UserName);

                DataTable dt = UserInfoProvider.GetUserRoles(userdata);

                if (dt.Rows.Count > 0 && dt != null)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["RoleName"].ToString().ToLower().Contains("peronifyuser"))
                        {
                            flagpersonifyMember = true;
                        }
                    }
                }

                ///if personify key is null or empty
                ///
                if (flagpersonifyMember)
                {
                    string tokenReturn = null;
                    if (Session[PersonifySessionKey] != null)
                    {
                        tokenReturn = this.ValidateCustomerToken(Session[PersonifySessionKey].ToString());
                    }

                    if (Session[PersonifySessionKey] == null)//|| tokenReturn == null)
                    {
                        /* Response.Redirect(
                         * Request.ServerVariables["PATH_INFO"] + "?action=logout&returnurl=" +
                         * Server.UrlEncode(Request.Url.AbsoluteUri), true);*/

                        //logout user
                        if (Session["userClass"] != null && Session["userClass"] != "")
                        {
                            /* userinfo ui = (userinfo)Session["userClass"];  //Session["userClass"];
                             * objKenticoService.Logout(ui.username);
                             * System.Web.Security.FormsAuthentication.SignOut();
                             * HttpContext.Current.Response.Cookies["ASPXFORMSAUTH"].Expires = DateTime.Now.AddYears(-1);
                             *
                             */
                            // Response.Redirect("/logout.aspx");
                        }
                    }
                }
            }
        }
    }