Ejemplo n.º 1
0
        public Model.Session CreateSession(string userId, SessionDurationRequest sessionDurationRequest)
        {
            var now = DateTime.UtcNow;
            var session = new Model.Session()
            {
                UserId = userId,
                StartDate = now,
                ExpirationDate = now
                                    .AddDays(sessionDurationRequest.Days)
                                    .AddHours(sessionDurationRequest.Hours)
                                    .AddMinutes(sessionDurationRequest.Minutes)
                                    .AddSeconds(sessionDurationRequest.Seconds)
            };

            _sessionStorageProvider.Add(session);
            SetCurrentSession(session);

            return session;
        }
        public string CreateAccessToken(string userId, UserAuthenticationRequest userAuthenticationRequest)
        {
            var sessionDurationRequest = new SessionDurationRequest()
            {
                Days = userAuthenticationRequest.Duration.Days,
                Hours = userAuthenticationRequest.Duration.Hours,
                Minutes = userAuthenticationRequest.Duration.Minutes,
                Seconds = userAuthenticationRequest.Duration.Seconds
            };

            var session = _sessionProvider.GetSession(userId);
            if (session == null)
            {
                session = _sessionProvider.CreateSession(userId, sessionDurationRequest);
            }
            else
            {
                session = _sessionProvider.ValidateSession(session);
            }

            return _encryptionProvider.Encrypt(JsonConvert.SerializeObject(session));
        }
        public Model.Session ValidateSession(Model.Session session)
        {
            var now = DateTime.UtcNow;
            var returnSession = _sessionProvider.ValidateSession(session);

            if (_sessionContext.EnableSessionRecycling)
            {
                if (returnSession == null)
                {
                    var graceSession = _sessionGraceStorageProvider.GetAll().FirstOrDefault(g => g.SessionId == session.Id && g.GraceStartDate <= now && g.GraceExpirationDate >= now);
                    if (graceSession == null)
                    {
                        return null;
                    }
                    returnSession = _sessionProvider.GetSession(session.UserId);
                    _sessionProvider.SetCurrentSession(returnSession);
                }
                else
                {
                    var currentSessionDuration = now - session.StartDate;
                    if (currentSessionDuration > _sessionContext.SessionRecyclingDuration)
                    {
                        var sessionGraceDuration = _sessionContext.SessionGraceDuration;
                        var graceSessionExpirationDate = now
                                                            .AddDays(sessionGraceDuration.Days)
                                                            .AddHours(sessionGraceDuration.Hours)
                                                            .AddMinutes(sessionGraceDuration.Minutes)
                                                            .AddSeconds(sessionGraceDuration.Seconds);
                        var graceSession = new SessionGrace()
                        {
                            SessionId = session.Id,
                            GraceStartDate = now,
                            GraceExpirationDate = graceSessionExpirationDate
                        };
                        _sessionGraceStorageProvider.Add(graceSession);
                        _sessionProvider.Logout(session.UserId);

                        var remainingSessionDuration = session.ExpirationDate - now;
                        var sessionDurationRequest = new SessionDurationRequest()
                        {
                            Days = remainingSessionDuration.Days,
                            Hours = remainingSessionDuration.Hours,
                            Minutes = remainingSessionDuration.Minutes,
                            Seconds = remainingSessionDuration.Seconds
                        };

                        returnSession = _sessionProvider.CreateSession(session.UserId, sessionDurationRequest);
                    }
                }
            }

            return returnSession;
        }
 public Model.Session CreateSession(string userId, SessionDurationRequest sessionDurationRequest)
 {
     return _sessionProvider.CreateSession(userId, sessionDurationRequest);
 }