Esempio n. 1
0
        private static SessionStateStoreData DeserializeSession(byte[] items, IEnumerable <byte> statics, int timeout)
        {
            SessionStateItemCollection        itemCol;
            const HttpStaticObjectsCollection StaticCol = null;

            using (var memoryStream = new MemoryStream(items))
            {
                using (var binaryReader = new BinaryReader(memoryStream))
                {
                    bool hasItems = binaryReader.ReadBoolean();
                    itemCol = hasItems ? SessionStateItemCollection.Deserialize(binaryReader) : new SessionStateItemCollection();
                }
            }

            if (HttpContext.Current != null && HttpContext.Current.Application != null &&
                HttpContext.Current.Application.StaticObjects != null && HttpContext.Current.Application.StaticObjects.Count > 0)
            {
                throw new ProviderException("This provider does not support static session objects because of security-related hosting constraints.");
            }

            if (statics != null && statics.Count() > 0)
            {
                throw new ProviderException("This provider does not support static session objects because of security-related hosting constraints.");
            }

            return(new SessionStateStoreData(itemCol, StaticCol, timeout));
        }
Esempio n. 2
0
            private static SessionStateStoreData Deserialize(
                HttpContext context,
                Stream stream)
            {
                int timeout;
                SessionStateItemCollection  stateItemCollection;
                HttpStaticObjectsCollection staticObjects;

                try
                {
                    BinaryReader reader = new BinaryReader(stream);
                    timeout = reader.ReadInt32();
                    bool flag = reader.ReadBoolean();
                    int  num  = reader.ReadBoolean() ? 1 : 0;
                    stateItemCollection = !flag ? new SessionStateItemCollection() : SessionStateItemCollection.Deserialize(reader);
                    staticObjects       = num == 0 ? SessionStateUtility.GetSessionStaticObjects(context) : HttpStaticObjectsCollection.Deserialize(reader);
                    if (reader.ReadByte() != byte.MaxValue)
                    {
                        throw new ProviderException(MsgManager.GetMsg(ErrRes.INVALID_SESSION_STATE));
                    }
                }
                catch (EndOfStreamException ex)
                {
                    throw new ProviderException(MsgManager.GetMsg(ErrRes.INVALID_SESSION_STATE));
                }
                return(new SessionStateStoreData((ISessionStateItemCollection)stateItemCollection, staticObjects, timeout));
            }
Esempio n. 3
0
        private static SessionStateStoreData DeserializeSession(byte[] items, byte[] statics, int timeout)
        {
            SessionStateItemCollection  itemCol   = null;
            HttpStaticObjectsCollection staticCol = null;

            using (MemoryStream stream1 = new MemoryStream(items))
            {
                using (BinaryReader reader1 = new BinaryReader(stream1))
                {
                    bool hasItems = reader1.ReadBoolean();
                    if (hasItems)
                    {
                        itemCol = SessionStateItemCollection.Deserialize(reader1);
                    }
                    else
                    {
                        itemCol = new SessionStateItemCollection();
                    }
                }
            }

            if (HttpContext.Current != null && HttpContext.Current.Application != null &&
                HttpContext.Current.Application.StaticObjects != null && HttpContext.Current.Application.StaticObjects.Count > 0)
            {
                throw new ProviderException("This provider does not support static session objects because of security-related hosting constraints.");
            }

            if (statics != null && statics.Count() > 0)
            {
                throw new ProviderException("This provider does not support static session objects because of security-related hosting constraints.");
            }

            return(new SessionStateStoreData(itemCol, staticCol, timeout));
        }
        public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            RedisConnection redisConnection         = this.GetRedisConnection();
            Task <Dictionary <string, byte[]> > all = redisConnection.Hashes.GetAll(0, this.GetKeyForSessionId(id), false);

            locked  = false;
            lockAge = new TimeSpan(0L);
            lockId  = (object)null;
            actions = SessionStateActions.None;
            if (all.Result == null)
            {
                return((SessionStateStoreData)null);
            }
            SessionStateItemCollection  stateItemCollection = new SessionStateItemCollection();
            Dictionary <string, byte[]> result = all.Result;

            if (result.Count == 3)
            {
                MemoryStream memoryStream = new MemoryStream(result["data"]);
                if (memoryStream.Length > 0L)
                {
                    stateItemCollection = SessionStateItemCollection.Deserialize(new BinaryReader((Stream)memoryStream));
                }
                int int32 = BitConverter.ToInt32(result["timeoutMinutes"], 0);
                redisConnection.Keys.Expire(0, this.GetKeyForSessionId(id), int32 * 60, false);
            }
            return(new SessionStateStoreData((ISessionStateItemCollection)stateItemCollection, SessionStateUtility.GetSessionStaticObjects(context), this.redisConfig.SessionTimeout));
        }
Esempio n. 5
0
        /// <summary>
        /// Deserialise the session state data.
        /// </summary>
        /// <param name="items">The bytes to deserialise.</param>
        /// <param name="statics">The http static object collection.</param>
        /// <param name="timeout">The timeout for deserialisation.</param>
        /// <returns>The session state object.</returns>
        private SessionStateStoreData DeserializeSession(byte[] items, byte[] statics, int timeout)
        {
            MemoryStream stream1 = null, stream2 = null;
            BinaryReader reader1 = null, reader2 = null;

            try
            {
                stream1 = new MemoryStream(items);
                stream2 = new MemoryStream(statics);
                reader1 = new BinaryReader(stream1);
                reader2 = new BinaryReader(stream2);

                return(new SessionStateStoreData(SessionStateItemCollection.Deserialize(reader1),
                                                 HttpStaticObjectsCollection.Deserialize(reader2), timeout));
            }
            finally
            {
                if (reader2 != null)
                {
                    reader2.Close();
                }
                if (reader1 != null)
                {
                    reader1.Close();
                }
                if (stream2 != null)
                {
                    stream2.Close();
                }
                if (stream1 != null)
                {
                    stream1.Close();
                }
            }
        }
Esempio n. 6
0
        public static SessionStateStoreData Deserialize(byte[] buffer)
        {
            MemoryStream stream = new MemoryStream(buffer);

            SessionStateItemCollection  itemCollection       = null;
            HttpStaticObjectsCollection staticItemCollection = null;
            int timeout = 0;

            try
            {
                BinaryReader reader = new BinaryReader(stream);


                byte sessionFlag = reader.ReadByte();

                if ((byte)(sessionFlag & SESSION_ITEMS) == SESSION_ITEMS)
                {
                    itemCollection = SessionStateItemCollection.Deserialize(reader);
                }
                if ((byte)(sessionFlag & SESSION_STATIC_ITEMS) == SESSION_STATIC_ITEMS)
                {
                    staticItemCollection = HttpStaticObjectsCollection.Deserialize(reader);
                }
                timeout = reader.ReadInt32();
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return(new SessionStateStoreData(itemCollection, staticItemCollection, timeout));
        }
Esempio n. 7
0
        private SessionStateStoreData GetSessionStateStoreData(bool exclusive, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            actions = SessionStateActions.None;
            lockAge = TimeSpan.Zero;
            locked  = false;
            lockId  = null;

            var query        = Query.And(Query.EQ("applicationVirtualPath", HostingEnvironment.ApplicationVirtualPath), Query.EQ("id", id));
            var bsonDocument = this.mongoCollection.FindOneAs <BsonDocument>(query);

            if (bsonDocument == null)
            {
                locked = false;
            }
            else if (bsonDocument["expires"].ToUniversalTime() <= DateTime.Now)
            {
                locked = false;
                this.mongoCollection.Remove(Query.And(Query.EQ("applicationVirtualPath", HostingEnvironment.ApplicationVirtualPath), Query.EQ("id", id)));
            }
            else if (bsonDocument["locked"].AsBoolean == true)
            {
                lockAge = DateTime.Now.Subtract(bsonDocument["lockDate"].ToUniversalTime());
                locked  = true;
                lockId  = bsonDocument["lockId"].AsInt32;
            }
            else
            {
                locked  = false;
                lockId  = bsonDocument["lockId"].AsInt32;
                actions = (SessionStateActions)bsonDocument["sessionStateActions"].AsInt32;
            }

            if (exclusive)
            {
                lockId  = (int)lockId + 1;
                actions = SessionStateActions.None;

                var update = Update.Set("lockDate", DateTime.Now).Set("lockId", (int)lockId).Set("locked", true).Set("sessionStateActions", SessionStateActions.None);
                this.mongoCollection.Update(query, update);
            }

            if (actions == SessionStateActions.InitializeItem)
            {
                return(this.CreateNewStoreData(context, this.sessionStateSection.Timeout.Minutes));
            }

            using (var memoryStream = new MemoryStream(bsonDocument["sessionStateItems"].AsByteArray))
            {
                var sessionStateItems = new SessionStateItemCollection();

                if (memoryStream.Length > 0)
                {
                    var binaryReader = new BinaryReader(memoryStream);
                    sessionStateItems = SessionStateItemCollection.Deserialize(binaryReader);
                }

                return(new SessionStateStoreData(sessionStateItems, SessionStateUtility.GetSessionStaticObjects(context), bsonDocument["timeout"].AsInt32));
            }
        }
 public ISessionStateItemCollection Deserialize(byte[] bytes)
 {
     using (var ms = new MemoryStream(bytes))
     {
         var reader = new BinaryReader(ms);
         return(SessionStateItemCollection.Deserialize(reader));
     }
 }
Esempio n. 9
0
 protected virtual SessionStateItemCollection DeserializeSessionState(byte[] bytes)
 {
     using (var ms = new MemoryStream(bytes))
         using (var br = new BinaryReader(ms))
         {
             SessionStateItemCollection result = SessionStateItemCollection.Deserialize(br);
             return(result);
         }
 }
Esempio n. 10
0
        public override void Skip(CompactBinaryReader reader)
        {
            int    cookie = reader.ReadInt32();
            object custom = reader.Context.GetObject(cookie);

            if (custom == null)
            {
                custom = SessionStateItemCollection.Deserialize(reader.BaseReader);
                reader.Context.RememberObject(custom, false);
            }
        }
        private static SessionStateValue DeserializeSessionState(BinaryReader reader)
        {
            var timeout          = reader.ReadInt32();
            var hasSessionItems  = reader.ReadBoolean();
            var hasStaticObjects = reader.ReadBoolean();

            return(new SessionStateValue(
                       hasSessionItems ? SessionStateItemCollection.Deserialize(reader) : null,
                       hasStaticObjects ? HttpStaticObjectsCollection.Deserialize(reader) : null,
                       timeout));
        }
        // DeSerialize is called by the GetSessionStoreItem method to
        // convert the Base64 string stored in the Access Memo field to a
        // SessionStateItemCollection.
        private SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout)
        {
            MemoryStream ms = new MemoryStream(Convert.FromBase64String(serializedItems));
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            if (ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }
            return(new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), timeout));
        }
        private SessionStateItemCollection Deserialize(byte[] serializedItems, int timeout)
        {
            MemoryStream ms = new MemoryStream(serializedItems);
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            if (ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }
            return(sessionItems);
        }
        private static SessionStateStoreData Deserialize(HttpContext context,
                                                         byte[] serializedItems, int timeout)
        {
            SessionStateItemCollection items =
                serializedItems != null && serializedItems.Length > 0 ?
                SessionStateItemCollection.Deserialize(
                    new BinaryReader(new MemoryStream(serializedItems))) :
                new SessionStateItemCollection();

            return(new SessionStateStoreData(items,
                                             SessionStateUtility.GetSessionStaticObjects(context), timeout));
        }
        /// <summary>
        /// 反序列化Session的数据
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public SessionStateItemCollection Deserialize(string item)
        {
            MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(item));
            SessionStateItemCollection collection = new SessionStateItemCollection();

            if (stream.Length > 0)
            {
                BinaryReader reader = new BinaryReader(stream);
                collection = SessionStateItemCollection.Deserialize(reader);
            }
            return(collection);
        }
        private SessionStateStoreData Deserialize(HttpContext context, byte[] bytes, int timeout)
        {
            MemoryStream stream = new MemoryStream(bytes);

            SessionStateItemCollection collection = new SessionStateItemCollection();

            if (stream.Length > 0)
            {
                BinaryReader reader = new BinaryReader(stream);
                collection = SessionStateItemCollection.Deserialize(reader);
            }
            return(new SessionStateStoreData(collection, SessionStateUtility.GetSessionStaticObjects(context), timeout));
        }
        /// <summary>
        /// Uses a <see cref="BinaryFormatter"/> to read an object of
        /// type <see cref="ActualType"/> from the underlying stream.
        /// </summary>
        /// <param name="reader">stream reader</param>
        /// <returns>object read from the stream reader</returns>
        public override object Read(CompactBinaryReader reader)
        {
            int    cookie = reader.ReadInt32();
            object custom = reader.Context.GetObject(cookie);

            if (custom == null)
            {
                // custom = formatter.Deserialize(reader.BaseReader.BaseStream);
                custom = SessionStateItemCollection.Deserialize(reader.BaseReader);
                reader.Context.RememberObject(custom, false);
            }
            return(custom);
        }
        /// <summary>
        /// Loads a session state item from the bucket. This function is publicly accessible
        /// so that you have direct access to session data from another application if necesssary.
        /// We use this so our front end code can determine if an employee is logged into our back
        /// end application to give them special permissions, without the session data being actually common
        /// between the two applications.
        /// </summary>
        /// <param name="headerPrefix">Prefix for the header data</param>
        /// <param name="dataPrefix">Prefix for the real data</param>
        /// <param name="bucket">Couchbase bucket to load from</param>
        /// <param name="id">Session ID</param>
        /// <param name="metaOnly">True to load only meta data</param>
        /// <returns>Session store item read, null on failure</returns>
        public static SessionStateItem Load(
            string headerPrefix,
            string dataPrefix,
            IBucket bucket,
            string id,
            bool metaOnly)
        {
            // Read the header value from Couchbase
            var header = bucket.Get <byte[]>(CouchbaseSessionStateProvider.HeaderPrefix + id);

            if (header.Status != ResponseStatus.Success)
            {
                return(null);
            }

            // Deserialize the header values
            SessionStateItem entry;

            using (var ms = new MemoryStream(header.Value))
            {
                entry = LoadHeader(ms);
            }
            entry.HeadCas = header.Cas;

            // Bail early if we are only loading the meta data
            if (metaOnly)
            {
                return(entry);
            }

            // Read the data for the item from Couchbase
            var data = bucket.Get <byte[]>(CouchbaseSessionStateProvider.DataPrefix + id);

            if (data.Value == null)
            {
                return(null);
            }
            entry.DataCas = data.Cas;

            // Deserialize the data
            using (var ms = new MemoryStream(data.Value))
            {
                using (var br = new BinaryReader(ms))
                {
                    entry.Data = SessionStateItemCollection.Deserialize(br);
                }
            }

            // Return the session entry
            return(entry);
        }
Esempio n. 19
0
        private SessionStateStoreData Deserialize(HttpContext context, Byte[] serializedItems, int timeout)
        {
            var ms = new MemoryStream(serializedItems);

            var sessionItems = new SessionStateItemCollection();

            if (ms.Length > 0)
            {
                var reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }

            return(new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), timeout));
        }
        private static SessionStateStoreData Deserialize(HttpContextBase context, Stream stream)
        {
            int timeout;
            SessionStateItemCollection sessionItems;
            bool hasItems;
            bool hasStaticObjects;
            HttpStaticObjectsCollection staticObjects;
            byte eof;

            Debug.Assert(context != null);

            try
            {
                BinaryReader reader = new BinaryReader(stream);

                timeout          = reader.ReadInt32();
                hasItems         = reader.ReadBoolean();
                hasStaticObjects = reader.ReadBoolean();

                if (hasItems)
                {
                    sessionItems = SessionStateItemCollection.Deserialize(reader);
                }
                else
                {
                    sessionItems = new SessionStateItemCollection();
                }

                if (hasStaticObjects)
                {
                    staticObjects = HttpStaticObjectsCollection.Deserialize(reader);
                }
                else
                {
                    staticObjects = GetSessionStaticObjects(context.ApplicationInstance.Context);
                }

                eof = reader.ReadByte();
                if (eof != 0xff)
                {
                    throw new HttpException(String.Format(CultureInfo.CurrentCulture, Resource1.Invalid_session_state));
                }
            }
            catch (EndOfStreamException)
            {
                throw new HttpException(String.Format(CultureInfo.CurrentCulture, Resource1.Invalid_session_state));
            }

            return(new SessionStateStoreData(sessionItems, staticObjects, timeout));
        }
Esempio n. 21
0
        private SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout)
        {
            SessionStateItemCollection sessionItems;

            using (MemoryStream memory = new MemoryStream(Convert.FromBase64String(serializedItems)))
            {
                using (BinaryReader reader = new BinaryReader(memory))
                {
                    sessionItems = SessionStateItemCollection.Deserialize(reader);
                }
            }

            return(new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), timeout));
        }
Esempio n. 22
0
        /// <summary>
        /// 反序列为集合
        /// </summary>
        /// <param name="binary">数据</param>
        /// <returns></returns>
        public static SessionStateItemCollection Deserialize(byte[] binary)
        {
            if (binary == null || binary.Length == 0)
            {
                return(new SessionStateItemCollection());
            }

            using (var ms = new MemoryStream(binary))
            {
                using (var reader = new BinaryReader(ms))
                {
                    return(SessionStateItemCollection.Deserialize(reader));
                }
            }
        }
Esempio n. 23
0
        private static SessionStateStoreData Deserialize(HttpContext context, Stream stream)
        {
            int  timeout;
            bool hasItems;
            bool hasStaticObjects;
            SessionStateItemCollection  sessionItems;
            HttpStaticObjectsCollection staticObjects;

            try
            {
                BinaryReader reader = new BinaryReader(stream);
                timeout          = reader.ReadInt32();
                hasItems         = reader.ReadBoolean();
                hasStaticObjects = reader.ReadBoolean();

                if (hasItems)
                {
                    sessionItems = SessionStateItemCollection.Deserialize(reader);
                }
                else
                {
                    sessionItems = new SessionStateItemCollection();
                }

                if (hasStaticObjects)
                {
                    staticObjects = HttpStaticObjectsCollection.Deserialize(reader);
                }
                else
                {
                    staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
                }

                if (reader.ReadByte() != EndOfStream)
                {
                    throw new HttpException(Resources.Invalid_session_state);
                }
            }
            catch (EndOfStreamException)
            {
                throw new HttpException(Resources.Invalid_session_state);
            }

            return(new SessionStateStoreData(sessionItems, staticObjects, timeout));
        }
        ///<summary>
        /// Deserialize is called by the GetSessionStoreItem method to
        /// convert the byte array stored in the blob field to a
        /// SessionStateItemCollection.
        /// </summary>
        private SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout)
        {
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            if (!string.IsNullOrEmpty(serializedItems))
            {
                byte[]       decrypt = Convert.FromBase64String(serializedItems);
                MemoryStream ms      = new MemoryStream(decrypt);
                if (ms.Length > 0)
                {
                    BinaryReader br = new BinaryReader(ms);
                    sessionItems = SessionStateItemCollection.Deserialize(br);
                    br.Close();
                    ms.Close();
                }
            }
            return(new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), timeout));
        }
        public static SessionStateItemCollection DeserializeCore(string serializedItems)
        {
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            MemoryStream ms = null;

            if (serializedItems != null && serializedItems.Trim().Length > 0)
            {
                ms = new MemoryStream(Convert.FromBase64String(serializedItems));
            }

            if (ms != null && ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }

            return(sessionItems);
        }
        /// <summary>
        /// Deserializes binary data into a fully hydrated SessionStoreDataContext.
        /// </summary>
        /// <param name="bytes">serialized binary data</param>
        /// <returns>The object representation of the session data</returns>
        /// <exception cref="HttpException">Thrown if deserialization encounters a problem</exception>
        internal static SessionStoreDataContext Deserialize(byte[] bytes, HttpContext context)
        {
            try
            {
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    using (BinaryReader reader = new BinaryReader(stream))
                    {
                        //See comments in Serialize method for description of fields
                        int      timeout          = reader.ReadInt32();
                        bool     hasItems         = reader.ReadBoolean();
                        bool     hasStaticObjects = reader.ReadBoolean();
                        bool     isLocked         = reader.ReadBoolean();
                        DateTime?lockDate         = null;
                        if (isLocked)
                        {
                            lockDate = DateTime.FromBinary(reader.ReadInt64());
                        }

                        SessionStateItemCollection items = hasItems
                            ? SessionStateItemCollection.Deserialize(reader)
                            : new SessionStateItemCollection();
                        HttpStaticObjectsCollection staticObjects = hasStaticObjects
                            ? HttpStaticObjectsCollection.Deserialize(reader)
                            : SessionStateUtility.GetSessionStaticObjects(context);

                        //this is a sanity byte.  If it does not equal 0xFF, there is a problem
                        if (reader.ReadByte() != 0xff)
                        {
                            throw new HttpException("Invalid Session State");
                        }

                        SessionStoreDataContext data = new SessionStoreDataContext(new SessionStateStoreData(items, staticObjects, timeout));
                        data.LockDate = lockDate;
                        return(data);
                    }
                }
            }
            catch (EndOfStreamException)
            {
                throw new HttpException("Invalid Session State");
            }
        }
            public static SessionStateItem Load(string headerPrefix, string dataPrefix, IMemcachedClient client, string id, bool metaOnly)
            {
                // Load the header for the item
                var header = client.GetWithCas <byte[]>(headerPrefix + id);

                if (header.Result == null)
                {
                    return(null);
                }

                // Deserialize the header values
                SessionStateItem entry;

                using (var ms = new MemoryStream(header.Result)) {
                    entry = SessionStateItem.LoadItem(ms);
                }
                entry.HeadCas = header.Cas;

                // Bail early if we are only loading the meta data
                if (metaOnly)
                {
                    return(entry);
                }

                // Load the data for the item
                var data = client.GetWithCas <byte[]>(dataPrefix + id);

                if (data.Result == null)
                {
                    return(null);
                }
                entry.DataCas = data.Cas;

                // Deserialize the data
                using (var ms = new MemoryStream(data.Result)) {
                    using (var br = new BinaryReader(ms)) {
                        entry.Data = SessionStateItemCollection.Deserialize(br);
                    }
                }

                // Return the session entry
                return(entry);
            }
        internal static SessionStateStoreData Deserialize(HttpContext context,
                                                          string serializedItems, int timeout)
        {
            using (var stream = new MemoryStream(Convert.FromBase64String(serializedItems)))
            {
                var sessionItems = new SessionStateItemCollection();

                if (stream.Length > 0)
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        sessionItems = SessionStateItemCollection.Deserialize(reader);
                    }
                }

                return(new SessionStateStoreData(sessionItems,
                                                 GetSessionStaticObjects(context),
                                                 timeout));
            }
        }
        /// <summary>
        /// Convert a Base64 string into a SessionStateItemCollection
        /// </summary>
        /// <param name="serializedItems"></param>
        /// <returns></returns>
        private static SessionStateItemCollection Deserialize(string serializedItems)
        {
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            if (string.IsNullOrEmpty(serializedItems))
            {
                return(sessionItems);
            }

            using (MemoryStream mStream = new MemoryStream(Convert.FromBase64String(serializedItems)))
            {
                using (BinaryReader bReader = new BinaryReader(mStream))
                {
                    sessionItems = SessionStateItemCollection.Deserialize(bReader);
                    bReader.Close();
                }
            }

            return(sessionItems);
        }
Esempio n. 30
0
        public override SessionStateStoreData GetItem(HttpContext context,
                                                      string id,
                                                      out bool locked,
                                                      out TimeSpan lockAge,
                                                      out object lockId,
                                                      out SessionStateActions actions)
        {
            var redis          = GetRedisConnection();
            var getSessionData = redis.Hashes.GetAll(0, GetKeyForSessionId(id));

            locked  = false;
            lockAge = new TimeSpan(0);
            lockId  = null;
            actions = SessionStateActions.None;

            if (getSessionData.Result == null)
            {
                return(null);
            }
            else
            {
                var sessionItems    = new SessionStateItemCollection();
                var sessionDataHash = getSessionData.Result;
                if (sessionDataHash.Count == 3)
                {
                    var ms = new MemoryStream(sessionDataHash["data"]);
                    if (ms.Length > 0)
                    {
                        var reader = new BinaryReader(ms);
                        sessionItems = SessionStateItemCollection.Deserialize(reader);
                    }

                    var timeoutMinutes = BitConverter.ToInt32(sessionDataHash["timeoutMinutes"], 0);
                    redis.Keys.Expire(0, GetKeyForSessionId(id), timeoutMinutes * 60);
                }

                return(new SessionStateStoreData(sessionItems,
                                                 SessionStateUtility.GetSessionStaticObjects(context),
                                                 redisConfig.SessionTimeout));
            }
        }