示例#1
0
        public void Should_Return_A_Null_Session()
        {
            var store   = new SqlSessionStateStore(Constants.ConnectionString, Constants.SessionTimeout);
            var session = store.Load(_sessionId);

            session.ShouldBeNull();
        }
        /// <summary>
        ///     Based on the session cookie from the current HTTP request, this method will look
        ///		in the session database for the UserId of the user currently logged in the user
        ///		provider of the application that calls this method.
        ///		Will return 0 if:
        ///			There is no session cookie on the HTTP request.
        ///			There is no user logged in with the same session cookie.
        ///			There is a user logged in, but only in a different user provider.
        /// </summary>
        /// <returns>The UserId of the user currently logged in.</returns>

        public static int GetUserFromSessionCookie()
        {
            string sessionId = GetCurrentSession();

            if (!sessionId.IsNullOrEmpty())
            {
                return(SqlSessionStateStore.GetUserId(sessionId));
            }
            return(0);
        }
示例#3
0
        public void Should_Abandon_Session()
        {
            SessionDatabase.CreateSession(Constants.FullSessionId, ShortData);
            var store = new SqlSessionStateStore(Constants.ConnectionString, Constants.SessionTimeout);

            store.Abandon(_sessionId);

            var session = SessionDatabase.GetSession(Constants.FullSessionId);

            session.ShouldBeNull();
        }
示例#4
0
        public void Should_Load_An_Empty_Session_And_Unlock_It_Afterwards()
        {
            SessionDatabase.CreateSession(Constants.FullSessionId, Constants.SessionStateEmptySerializedBytes);
            var store        = new SqlSessionStateStore(Constants.ConnectionString, Constants.SessionTimeout);
            var sessionState = store.Load(_sessionId);

            sessionState.ShouldEqual(Constants.SessionStateEmptySerializedBytes);

            var session = SessionDatabase.GetSession(Constants.FullSessionId);

            session.ShouldNotBeNull("No session found");
            session.Locked.ShouldBeFalse();
        }
示例#5
0
        public void Should_Concurrently_Load_A_Session_And_Unlock_It_Afterwards()
        {
            SessionDatabase.CreateSession(Constants.FullSessionId, ShortData);
            var store   = new SqlSessionStateStore(Constants.ConnectionString, Constants.SessionTimeout);
            var results = new ConcurrentBag <bool>();

            for (var i = 0; i < 20; i++)
            {
                ThreadPool.QueueUserWorkItem(
                    x => results.Add((store.Load(_sessionId) ?? new byte[] {}).SequenceEqual(ShortData)));
            }

            while (results.Count < 20)
            {
                Thread.Sleep(100);
            }

            results.All(x => x).ShouldBeTrue();
        }
示例#6
0
        public void Should_Write_New_Session_With_Long_Data()
        {
            var store = new SqlSessionStateStore(Constants.ConnectionString, Constants.SessionTimeout);

            store.Save(_sessionId, LongData);

            var session = SessionDatabase.GetSession(Constants.FullSessionId);

            session.ShouldNotBeNull("No session found");
            session.Locked.ShouldBeFalse();
            session.Created.ShouldBeInRange(DateTime.UtcNow.AddSeconds(-5), DateTime.UtcNow.AddSeconds(5));
            session.Expires.ShouldBeInRange(DateTime.UtcNow.AddMinutes(20).AddSeconds(-5), DateTime.UtcNow.AddMinutes(20).AddSeconds(5));
            session.LockDate.ShouldBeInRange(DateTime.UtcNow.AddSeconds(-5), DateTime.UtcNow.AddSeconds(5));
            session.LockDateLocal.ShouldBeInRange(DateTime.Now.AddSeconds(-5), DateTime.Now.AddSeconds(5));
            session.LockCookie.ShouldEqual(1);
            session.Timeout.ShouldEqual(20);
            session.SessionItemShort.ShouldBeNull();
            session.SessionItemLong.ShouldEqual(LongData);
            session.Flags.ShouldEqual(0);
        }