public UserAuthenticationInfoViewModel Execute()
        {
            Domain.Users.Authentication.User domainUser = this.userRepository.GetByCredentials(this.UserLoginInfo.Email, this.UserLoginInfo.Password);

            if (domainUser.IsBanned)
            {
                throw new UserIsBannedException();
            }

            if (domainUser == null)
            {
                throw new UserNotFoundException();
            }

            var userViewModel = new UserAuthenticationInfoViewModel
            {
                Name  = domainUser.FirstName,
                Email = this.UserLoginInfo.Email,
                Role  = domainUser.Role.ToString()
            };

            userViewModel.CreateToken(this.appSettings.Secret);

            return(userViewModel);
        }
示例#2
0
        public UserAuthenticationInfoViewModel Execute()
        {
            User user = Mapper.Map <User>(this.CheckinModel);

            user.Role     = Roles.User;
            user.IsBanned = false;

            try
            {
                this.userRepository.Create(user);
            }
            catch (MongoWriteException ex)
            {
                throw new DuplicateLoginException($"User with login {user.Email} already exist.");
            }

            var userViewModel = new UserAuthenticationInfoViewModel
            {
                Name  = user.FirstName,
                Email = user.Email,
                Role  = user.Role.ToString()
            };

            userViewModel.CreateToken(this.appSettings.Secret);

            return(userViewModel);
        }
示例#3
0
        public static void CreateToken(this UserAuthenticationInfoViewModel loginInfo, string secretKey)
        {
            var tokenHandler     = new JwtSecurityTokenHandler();
            var key              = Encoding.ASCII.GetBytes(secretKey);
            var tokenDescription = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, loginInfo.Email),
                    new Claim(ClaimTypes.Email, loginInfo.Name),
                    new Claim(ClaimTypes.Role, loginInfo.Role)
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescription);

            loginInfo.Token = tokenHandler.WriteToken(token);
        }