public static void RestoreSessionFromCookie(this HttpSessionState Session) { try { SetSessionTypes(); var coockieValue = EncryptHelpers.Desencriptar(CookieHelpers.GetValue(SessionKey._appBP)); var dictSessionObject = new Dictionary <string, object>(); dictSessionObject = JsonConvert.DeserializeObject <Dictionary <String, object> >(coockieValue); foreach (var item in dictSessionObject) { if (item.Value != null) { var sessionKey = item.Key.ToEnum <SessionKey>(); if (sessionKey.Equals(SessionKey._appBP)) { continue; } else if (sessionKey.Equals(SessionKey.Rol)) { Set(Session, sessionKey, item.Value.ToString().ToEnum <AppRol>()); } else if (lstCast.Any(x => x.Item1 == sessionKey)) { var cast = lstCast.FirstOrDefault(x => x.Item1 == sessionKey); var value = JsonConvert.DeserializeObject(item.Value.ToString(), cast.Item2); Set(Session, sessionKey, value); } else { Set(Session, sessionKey, item.Value); } } } } catch (Exception) { Session.Clear(); CookieHelpers.DeleteAll(); } }
/// <summary> /// Serializa Coockies Dependiendo de los /// </summary> /// <param name="Session"></param> public static void SetCookie(this HttpSessionStateBase Session) { try { var lstSessionKey = Session.Keys; var sessionKeyNames = Enum.GetNames(typeof(SessionKey)).ToList(); var dictSessionObject = sessionKeyNames.ToDictionary(x => x, v => new object()); foreach (var key in dictSessionObject.Keys.ToList()) { dictSessionObject[key] = null; } for (int i = 0; i <= Session.Count - 1; i++) { var key = Session.Keys[i]; if (sessionKeyNames.Any(x => x.Equals(key))) { if (key.Equals(SessionKey.Rol.ToString())) { dictSessionObject[key] = Session[key].ToString(); } else { dictSessionObject[key] = Session[key]; } } } var result = JsonConvert.SerializeObject(dictSessionObject, new JsonSerializerSettings() { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ObjectCreationHandling = ObjectCreationHandling.Reuse }); var resultEncriptado = EncryptHelpers.Encriptar(result); CookieHelpers.Set(SessionKey._appBP, resultEncriptado); } catch (Exception ex) { throw ex; } }