示例#1
0
        public async Task SignUpAsync(string email, string password)
        {
            var userDomain = (User)null;

            if (Shared.UseSql)
            {
                userDomain = await _userEFRepository.GetAsync(email);
            }
            else
            {
                userDomain = await _userMongoRepository.GetAsync(email);
            }

            if (userDomain != null)
            {
                throw new NextDoorException(IdentityExceptionCode.EmailInUse,
                                            $"Email: '{email}' is already in use.");
            }

            var userDto = new UserDto(email, password);

            userDto.SetHashPassword(_passwordHasher);
            userDomain = _mapper.Map <UserDto, User>(userDto);

            if (Shared.UseSql)
            {
                await _userEFRepository.AddAsync(userDomain);
            }
            else
            {
                await _userMongoRepository.AddAsync(userDomain);
            }
        }
        public async Task HandleAsync(SignUpCmd command, ICorrelationContext context)
        {
            var userDomain = (User)null;

            if (Shared.UseSql)
            {
                userDomain = await _userEFRepository.GetAsync(command.Email);
            }
            else
            {
                userDomain = await _userMongoRepository.GetAsync(command.Email);
            }
            if (userDomain != null)
            {
                throw new NextDoorException(IdentityExceptionCode.EmailInUse,
                                            $"Email: '{command.Email}' is already in use.");
            }
            else
            {
                userDomain = new User(command.Email);
                userDomain.SetPassword(command.Password, _passwordHasher);
                if (Shared.UseSql)
                {
                    await _userEFRepository.AddAsync(userDomain);
                }
                else
                {
                    await _userMongoRepository.AddAsync(userDomain);
                }

                // Notify some other service that sign up successed
                await _busPublisher.PublishAsync(new SignUpSuccessEvent(userDomain.Email), context);
            }
        }
示例#3
0
        public async Task <RefreshTokenDto> CreateNewRefreshTokenAsync(int Uid)
        {
            var userDomain = (User)null;

            if (Shared.UseSql)
            {
                userDomain = await _userEFRepository.GetAsync(Uid);
            }
            else
            {
                userDomain = await _userMongoRepository.GetAsync(Uid);
            }

            if (userDomain == null)
            {
                throw new NextDoorException(IdentityExceptionCode.UserNotFound,
                                            $"User was not found.");
            }

            // Get User Information to create hashed token for RefreshToken's Token
            var userDto         = _mapper.Map <User, UserDto>(userDomain);
            var refreshTokenDto = new RefreshTokenDto(userDto, _passwordHasher);

            refreshTokenDto.Role = userDto.Role;

            // Insert into Database
            var refreshTokenDomain = _mapper.Map <RefreshTokenDto, RefreshToken>(refreshTokenDto);

            if (Shared.UseSql)
            {
                await _refreshTokenEFRepository.AddAsync(refreshTokenDomain);
            }
            else
            {
                await _refreshTokenMongoRepository.AddAsync(refreshTokenDomain);
            }

            return(refreshTokenDto);
        }
示例#4
0
        public async Task <JsonWebToken> HandleAsync(SignInQuery query)
        {
            var userDomain = (User)null;

            if (Shared.UseSql)
            {
                userDomain = await _userEFRepository.GetAsync(query.Email);
            }
            else
            {
                userDomain = await _userMongoRepository.GetAsync(query.Email);
            }

            if (userDomain == null)
            {
                throw new NextDoorException(IdentityExceptionCode.InvalidEmail,
                                            $"Email: {query.Email} is invalid.");
            }
            else
            {
                if (!userDomain.ValidatePassword(query.Password, _passwordHasher))
                {
                    throw new NextDoorException(IdentityExceptionCode.InvalidCredentials,
                                                "Invalid credentials.");
                }

                await _tokenService.RevokeAllExistedRefreshTokenAsync(userDomain.Id);

                await _identityService.UpdateLastLogin(userDomain);

                // Create New Refresh Token
                var refreshTokenDto = await _tokenService.CreateNewRefreshTokenAsync(userDomain.Id);

                // Create New Jwt Access Token and bind with Refresh Token
                var jwt = await _tokenService.CreateNewJwtAccessTokenAsync(userDomain.Id, userDomain.Role, refreshTokenDto.Token);

                return(jwt);
            }
        }