private static void RequestParmToCookie(HttpContext context, WebSession webSession, string parmName, string slotName)
        {
            if (context.Request[parmName] == null)
            {
                return;
            }

            var parmValue = context.Request[parmName];

            if (context.Request.Cookies[parmName] == null ||
                !context.Request.Cookies[parmName].ToString().Equals(parmValue, StringComparison.InvariantCultureIgnoreCase))
            {
                var cookie = new HttpCookie(parmName, parmValue)
                {
                    HttpOnly = true
                };
                context.Response.Cookies.Set(cookie);
            }

            if (webSession != null)
            {
                webSession.Set(slotName, parmValue);
            }
        }
 private static void AddWebSession(Guid requestId, WebSession webSession)
 {
     _webSessions.TryAdd(requestId, webSession);
 }
        public static void BeginSession(HttpContext context, ICrypto cryptographicService)
        {
            SessionToken sessionToken         = null;
            bool         isCookieDecrypted    = false;
            bool         isSessionTokenParsed = false;
//            bool isNewSession = false;

            // we must set this context value before trying to get site info because site info may be affected by it
            var mktp = (string)context.Items[MarketPlaceParameter];

            if (String.IsNullOrEmpty(mktp))
            {
                var mktpCookie = context.Request.Cookies.Get(MarketPlaceParameter);
                if (mktpCookie != null)
                {
                    context.Items[MarketPlaceParameter] = mktpCookie.Value;
                }
            }

            SiteInfo siteInfo;

            var sessionCookie = context.Request.Cookies.Get(SessionTokenCookieName);

            if (sessionCookie != null)
            {
                string decryptedSessionCookieValue;
                isCookieDecrypted    = cryptographicService.TryDecrypt(sessionCookie.Value, out decryptedSessionCookieValue);
                isSessionTokenParsed = SessionToken.TryParse(decryptedSessionCookieValue, out sessionToken);
            }

            var destroySecurityToken = false;

            if (TryGetSiteInfo(context, out siteInfo))
            {
                if (sessionCookie != null && isCookieDecrypted && isSessionTokenParsed)
                {
                    if (!String.Equals(sessionToken.SiteId, siteInfo.SiteId) ||
                        !String.Equals(sessionToken.CultureCode, siteInfo.Locale) ||
                        !String.Equals(sessionToken.CountryCode, siteInfo.Country))
                    {
                        var preSessionId = PreSessionId();
                        sessionToken = new SessionToken(siteInfo.SiteId, siteInfo.Locale, siteInfo.Country,
                                                        siteInfo.Currency, context.GetExternalId(), preSessionId);

                        destroySecurityToken = true;
//                        isNewSession = true;
                    }
                }
                else
                {
                    var preSessionId = !String.IsNullOrEmpty(siteInfo.SiteId)
                        ? PreSessionId()
                        : null;

                    if (preSessionId == null)
                    {
                        sessionToken = new SessionToken(siteInfo.SiteId, null, null, null, context.GetExternalId(), null);
//                        isNewSession = true;
                    }
                    else
                    {
                        sessionToken = new SessionToken(siteInfo.SiteId, siteInfo.Locale, siteInfo.Country,
                                                        siteInfo.Currency, context.GetExternalId(), preSessionId);
                    }

                    destroySecurityToken = true;
                }
            }

            WebSession session = null;

            if (sessionToken != null)
            {
                session = new WebSession();
                session.Set(SessionTokenSlot, sessionToken);
//                session.IsNewSession = isNewSession;
                if (siteInfo != null)
                {
                    session.Set(SiteInfoSlot, siteInfo);
                }
            }


            RequestParmToCookie(context, session, McIdCookieName, McIdSlot);
            RequestParmToCookie(context, session, IcIdCookieName, IcIdSlot);

            if (destroySecurityToken)
            {
                if (context.Request.Cookies[SecurityTokenCookieName] != null)
                {
                    context.Response.Cookies.Remove(SecurityTokenCookieName);
                }
            }
            else
            {
                var    securityTokenCookie = context.Request.Cookies.Get(SecurityTokenCookieName);
                string decryptedSecurityTokenCookieValue;
                if (session != null && securityTokenCookie != null &&
                    cryptographicService.TryDecrypt(securityTokenCookie.Value, out decryptedSecurityTokenCookieValue))
                {
                    session.Set(SecurityTokenSlot, JsonConvert.DeserializeObject <SecurityToken>(decryptedSecurityTokenCookieValue));
                }
            }

            Current = session;
            if (session == null)
            {
                return;
            }

            var persistentPropertiesCookie = context.Request.Cookies.Get(PersistentPropertiesCookieName);

            if (persistentPropertiesCookie == null)
            {
                return;
            }

            string decryptedPersistentPropertiesCookieValue;

            if (cryptographicService.TryDecrypt(persistentPropertiesCookie.Value,
                                                out decryptedPersistentPropertiesCookieValue))
            {
                session.PersistentProperties =
                    JsonConvert.DeserializeObject <Dictionary <string, string> >(decryptedPersistentPropertiesCookieValue);
            }
        }