예제 #1
0
        /// <summary>
        /// Record a login event for a user, run any needed cleanup, and give the user a session.
        /// </summary>
        public void Login(DateTime now)
        {
            // Kill the anonymous cookie
            Current.KillCookie(Current.AnonymousCookieName);

            // Write a login event
            var loginEvent = new UserHistory
            {
                Comment           = "Logged In",
                CreationDate      = now,
                UserHistoryTypeId = UserHistoryTypeId.Login,
                UserId            = Id,
                IP = Current.RemoteIP
            };

            Current.WriteDB.UserHistory.InsertOnSubmit(loginEvent);

            // Generate and write a session
            var session     = Convert.ToBase64String(Current.Random(32));
            var sessionHash = Current.WeakHash(session);

            SessionHash         = sessionHash;
            SessionCreationDate = now;
            LastActivityDate    = now;

            // Add the user session cookie
            Current.AddCookie(Current.UserCookieName, session, TimeSpan.FromDays(7));

            Current.WriteDB.SubmitChanges();
        }