Пример #1
0
        public static void SetSessionValue(string sessionProp, object sessionValue)
        {
            string sessionValueStr = string.Empty;
            bool   rememberPwd     = false;

            if (sessionValue != null)
            {
                sessionValueStr = sessionValue.ToString();
            }

            IDictionary <string, string> sessionObjDict = new Dictionary <string, string>();

            if (HttpContext.Current.Session != null && HttpContext.Current.Session[Constants.SessionObjects] != null)
            {
                sessionObjDict = DecryptSessionObject((string)HttpContext.Current.Session[Constants.SessionObjects]);
            }

            if (sessionObjDict != null && !sessionObjDict.ContainsKey(sessionProp))
            {
                sessionObjDict.Add(sessionProp, sessionValueStr);
            }
            else
            {
                sessionObjDict[sessionProp] = sessionValueStr;
            }

            string encryptedSessionDict = EncryptSessionObject(sessionObjDict);

            HttpContext.Current.Session[Constants.SessionObjects] = encryptedSessionDict;

            //Add Session Items to cookies Container
            if (sessionObjDict != null && sessionObjDict.Any() && sessionObjDict.ContainsKey(SessionItemKey.EmailAddress))
            {
                CookieSessionItems _cookieSessionItems = new CookieSessionItems
                {
                    EmailAddress = sessionObjDict[SessionItemKey.EmailAddress] != null ? sessionObjDict[SessionItemKey.EmailAddress] : null,
                    //EncryptedSession = encryptedSessionDict,
                    SessionObject = ObjectToString(sessionObjDict)
                };

                //Add a cookie for remember, if remember me checked, else delete the cookie
                if (rememberPwd)
                {
                    _cookieSessionItems.RememberMe = rememberPwd;
                    AppCookies.SetValue(SessionItemKey.EmailAddress, _cookieSessionItems.EmailAddress, DateTime.MaxValue);
                    AppCookies.SetValue(SessionItemKey.CookieSession, HttpUtility.UrlEncode(Utility.Serialize(_cookieSessionItems)), DateTime.MaxValue);
                }
                else
                {
                    _cookieSessionItems.RememberMe = rememberPwd;
                    AppCookies.SetValue(SessionItemKey.EmailAddress, _cookieSessionItems.EmailAddress);
                    AppCookies.SetValue(SessionItemKey.CookieSession, HttpUtility.UrlEncode(Utility.Serialize(_cookieSessionItems)), DateTime.MaxValue);
                }
            }
        }
Пример #2
0
        public static void SetSessionObject(SessionObjects sObject)
        {
            IDictionary <string, string> sessionDict = new Dictionary <string, string>();
            bool rememberPwd = false;

            // We called this inspectionReportfields because the entity properties correspond to form/report fields I'm generating from this data.
            var sessionProperties = sObject.GetType().GetProperties();

            foreach (var prop in sessionProperties)
            {
                var sessionValue = (prop.GetValue(sObject) != null ? prop.GetValue(sObject).ToString() : null);
                if (!string.IsNullOrEmpty(sessionValue))
                {
                    sessionDict.Add(prop.Name, (prop.GetValue(sObject) != null ? prop.GetValue(sObject).ToString() : null));
                }
            }

            if (HttpContext.Current.Session != null)
            {
                // assign the updated sessionObject to current session by Encrypting SessionObject to String
                string encryptedSessionDict = EncryptSessionObject(sessionDict);

                HttpContext.Current.Session[Constants.SessionObjects] = encryptedSessionDict;

                //Add Session Items to cookies Container
                if (sessionDict != null && sessionDict.Any() && sessionDict.ContainsKey(SessionItemKey.EmailAddress))
                {
                    CookieSessionItems _cookieSessionItems = new CookieSessionItems
                    {
                        EmailAddress = sessionDict[SessionItemKey.EmailAddress] != null ? sessionDict[SessionItemKey.EmailAddress] : null,
                        //EncryptedSession = encryptedSessionDict,
                        SessionObject = ObjectToString(sessionDict)
                    };

                    //Add a cookie for remember, if remember me checked, else delete the cookie
                    if (rememberPwd)
                    {
                        _cookieSessionItems.RememberMe = rememberPwd;
                        AppCookies.SetValue(SessionItemKey.EmailAddress, _cookieSessionItems.EmailAddress, DateTime.MaxValue);
                        AppCookies.SetValue(SessionItemKey.CookieSession, HttpUtility.UrlEncode(Utility.Serialize(_cookieSessionItems)), DateTime.MaxValue);
                    }
                    else
                    {
                        _cookieSessionItems.RememberMe = rememberPwd;
                        AppCookies.SetValue(SessionItemKey.EmailAddress, _cookieSessionItems.EmailAddress);
                        AppCookies.SetValue(SessionItemKey.CookieSession, HttpUtility.UrlEncode(Utility.Serialize(_cookieSessionItems)), DateTime.MaxValue);
                    }
                }
            }
        }