示例#1
0
        public ISession Login(string username, string password)
        {
            Exist();
            CheckPw(password);
            CheckUserName(username);
            try
            {
                using (var c = new UserSessionContext(_connectionString))
                {
                    var userMatched =
                        (from u in c.Users where u.Name == username && u.SiteName == Name select u)
                        .SingleOrDefault();

                    if (userMatched == null ||
                        !VerifyHashedPw(userMatched.Password, password, HashedPwSize, SaltSize, IterationNumber))
                    {
                        return(null);
                    }

                    var dbSession = (from s in c.Sessions
                                     where s.User.Name == userMatched.Name //prendo la sessione più recente
                                     select s).OrderByDescending(s => s.ValidUntil).FirstOrDefault();
                    ISession session;
                    if (dbSession != null && dbSession.ValidUntil > _alarmClock.Now)                   //se la sessione esiste ed è ancora valida
                    {
                        dbSession.ValidUntil = _alarmClock.Now.AddSeconds(SessionExpirationInSeconds); //aggiorno la sessione a db
                        c.SaveChanges();
                        var user = new User(username, Name, _alarmClock, _connectionString);
                        session = new Session(dbSession.Id, dbSession.ValidUntil, user, _alarmClock,
                                              _connectionString);
                        return(session);
                    }
                    else
                    {
                        var newSession = new SessionEntity()
                        {
                            Id         = GenerateSessionId(),
                            ValidUntil = _alarmClock.Now.AddSeconds(SessionExpirationInSeconds)
                        };
                        userMatched.Sessions.Add(newSession);
                        c.Sessions.Add(newSession);
                        c.SaveChanges();
                        var user = new User(username, Name, _alarmClock, _connectionString);
                        session = new Session(newSession.Id, newSession.ValidUntil, user, _alarmClock, _connectionString);
                        return(session);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw new UnavailableDbException("connection failed!", e);
            }
        }
        public UserSessionController(UserSessionContext context)
        {
            _context = context;

            if (!_context.UserSessions.Any())
            {
                _context.UserSessions.Add(new UserSession {
                    UserID = 1, SessionID = 1
                });
                _context.SaveChanges();
            }
        }
        public ActionResult PostUserSession([FromBody] UserSession userSession)
        {
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            try
            {
                _context.UserSessions.Add(userSession);
                _context.SaveChanges();
                return(Json(userSession));
            } catch (Exception e)
            {
            }



            return(Ok());
        }
示例#4
0
        public void CleanupSessions()
        {
            Exist();
            try {
                using (var c = new UserSessionContext(_connectionString))
                {
                    var sessions = (from u in c.Users where u.SiteName == Name select u.Sessions).ToList().AsReadOnly();
                    foreach (var session in from x in sessions from session in x
                             where session.ValidUntil < _alarmClock.Now select session)
                    {
                        c.Sessions.Remove(session);
                    }

                    c.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw new UnavailableDbException("connection failed!", e);
            }
        }