Esempio n. 1
0
        public TokenEntity GetToken(UserEntity user)
        {
            try
            {
                if (user != null && user.Id >= 0)
                {
                    if (user.Enabled)
                    {
                        if (user.Verified)
                        {
                            var dateTime = DateTime.UtcNow;
                            var token    = tokensRepository.FindBy(x => x.UserId.Equals(user.Id) && x.DateTimeExpire > dateTime).OrderByDescending(x => x.DateTimeCreated).FirstOrDefault();

                            if (token == null)
                            {
                                var lifeSpan    = double.Parse(Properties.Resources.TokenLifeSpanMinutes);
                                var expire      = dateTime.AddMinutes(lifeSpan);
                                var tokenString = user.GenerateToken(Properties.Resources.BackEndKey, dateTime, expire);

                                var newToken = tokensRepository.Add(new TokenEntity
                                {
                                    String          = tokenString,
                                    UserId          = user.Id,
                                    DateTimeCreated = dateTime,
                                    DateTimeExpire  = expire,
                                    User            = user
                                });

                                this.unitOfWork.Save();

                                return(newToken);
                            }

                            return(token);
                        }

                        throw new GeneralException(10603, string.Format("User with the email address '{0}' has been marked as not verified!", user.Email.Trim().ToLower()));
                    }

                    throw new GeneralException(10602, string.Format("User with the email address '{0}' has been marked as disabled!", user.Email.Trim().ToLower()));
                }

                throw new GeneralException(10601, string.Format("User with the email address '{0}' does not exists!", user?.Email?.Trim().ToLower()));
            }
            catch (Exception exception)
            {
                throw;
            }
        }