public async Task <Result <bool> > Handle(VerifyEmailCommand request, CancellationToken cancellationToken)
        {
            var errors = Validate(request);

            if (errors.Any())
            {
                return(new Result <bool>(false, default, errors));
Exemple #2
0
        public async Task <IActionResult> OnPostAsync([FromBody] VerifyEmailDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(await Task.FromResult(new JsonResult(new
                {
                    Status = false,
                    ErrorMessage = ModelState.Where(e => e.Value.Errors.Count > 0).Select(e => e.Value.Errors.First().ErrorMessage).First()
                })));
            }

            var userId = _accountContext.UserId;

            var command = new VerifyEmailCommand(dto.Email, dto.Code);
            await _bus.SendCommand(command);

            if (_notifications.HasNotifications())
            {
                var errorMessage = string.Join(";", _notifications.GetNotifications().Select(x => x.Content));
                return(await Task.FromResult(new JsonResult(new
                {
                    status = false,
                    errorMessage
                })));
            }

            return(await Task.FromResult(new JsonResult(new
            {
                status = true
            })));
        }
        public ActionResult VerifyEmail(string email, string verificationCode)
        {
            var verifyEmailCommand = new VerifyEmailCommand(email, verificationCode);

            ExecuteCommand(verifyEmailCommand);

            return(View(verifyEmailCommand.Result));
        }
Exemple #4
0
        private HttpContent GetHttpContent <T>(VerifyEmailCommand <T> command)
        {
            var requestBody = new
            {
                TokenValue = command.TokenValue,
                Version    = command.Version
            };

            return(new StringContent(this.serializerService.Serialize(requestBody)));
        }
Exemple #5
0
        public async Task <Unit> Handle(VerifyEmailCommand command, CancellationToken cancellationToken)
        {
            var email = command.Email;
            var code  = command.Code;
            var user  = await _userDomainService.Get(p => p.Email == email);

            if (user == null)
            {
                await _bus.RaiseEvent(new DomainNotification("输入信息有误,请确认邮箱是否无误"));

                return(Unit.Value);
            }

            if (user.HasVerifiedEmail)
            {
                await _bus.RaiseEvent(new DomainNotification("邮箱已经验证通过,无需再次验证"));

                return(Unit.Value);
            }

            string key = string.Format(RedisKey.VerifyEmail, user.Email);

            var verifyEmailCode = await _redisDb.StringGet <string>(key);

            if (string.IsNullOrEmpty(verifyEmailCode))
            {
                await _bus.RaiseEvent(new DomainNotification("验证码错误,验证失败"));

                return(Unit.Value);
            }

            if (code != verifyEmailCode)
            {
                await _bus.RaiseEvent(new DomainNotification("验证码错误,验证失败"));

                return(Unit.Value);
            }

            await _redisDb.KeyDelete(key);

            user.HasVerifiedEmail = true;
            await _userDomainService.Update(user);


            if (await Commit())
            {
                await _bus.RaiseEvent(new VerifyEmailEvent(user)).ConfigureAwait(false);
            }


            return(Unit.Value);
        }
Exemple #6
0
        public async Task <ActionResult> VerifyEmail(string email)
        {
            try {
                var command = new VerifyEmailCommand {
                    Email = email
                };
                await _commandHandler.HandleAsync(command);

                return(Ok());
            } catch (Exception) {
                return(BadRequest());
            }
        }
        public async Task Handle_Should_ReturnErrorIfUserNotFound()
        {
            _emailValidatorMock.Setup(m => m.Validate(It.IsAny <string>()))
            .Returns(true);

            var command = new VerifyEmailCommand("*****@*****.**", "code");

            var result = await _handler.Handle(command, default);

            result.Should().NotBeNull();
            result.Value.Should().BeFalse();
            result.ErrorMessages.Should().Contain(VerifyEmailCommandHandler.Unable_To_Verify);
        }
    public async Task <IActionResult> VerifyEmail([FromBody] VerifyEmailDto verifyEmailDto)
    {
        if (!ModelState.IsValid)
        {
            return(new BadRequestObjectResult(ModelState));
        }

        var command = new VerifyEmailCommand(verifyEmailDto.Email, verifyEmailDto.Code);
        var result  = await _mediatr.Send(command);

        if (result.Value == false)
        {
            return(new BadRequestObjectResult(result.ErrorMessages));
        }

        return(new OkResult());
    }
        public async Task Handle_Should_SucceedForVerifiedUser()
        {
            var user = new User("*****@*****.**", "firstname", "lastname", true, "salt", "password", DateTime.UtcNow, DateTime.UtcNow);

            _emailValidatorMock.Setup(m => m.Validate(It.IsAny <string>()))
            .Returns(true);

            _userRepositoryMock.Setup(m => m.Get(It.IsAny <string>()))
            .ReturnsAsync(user);

            var command = new VerifyEmailCommand("*****@*****.**", "code");

            var result = await _handler.Handle(command, default);

            result.Should().NotBeNull();
            result.Value.Should().BeTrue();
            result.ErrorMessages.Should().BeEmpty();
        }
Exemple #10
0
        public async Task HandleAsync(VerifyEmailCommand command)
        {
            var user = await _repository.GetByEmail(command.Email);

            if (user == null)
            {
                throw new EntityNotFoundException($"User with email '{command.Email}' Not Found");
            }

            if (user.EmailVerified)
            {
                throw new Exception($"User has already verified email '{command.Email}'.");
            }

            user.EmailVerified = true;

            await _repository.Update(user);

            await _commandStoreService.PushAsync(command);
        }
        public async Task Handle_Should_ReturnErrorWhenCodeDoNotIsIncorrect(string code)
        {
            var user = new User("*****@*****.**", "firstname", "lastname", false, "salt", "password", DateTime.UtcNow, DateTime.UtcNow);

            _emailValidatorMock.Setup(m => m.Validate(It.IsAny <string>()))
            .Returns(true);

            _userRepositoryMock.Setup(m => m.Get(It.IsAny <string>()))
            .ReturnsAsync(user);

            _emailVerificationRepositoryMock.Setup(m => m.GetCode(It.IsAny <string>()))
            .ReturnsAsync(code);

            var command = new VerifyEmailCommand("*****@*****.**", "code");

            var result = await _handler.Handle(command, default);

            result.Should().NotBeNull();
            result.Value.Should().BeFalse();
            result.ErrorMessages.Should().Contain(VerifyEmailCommandHandler.Unable_To_Verify);
        }
Exemple #12
0
        public async Task <IActionResult> VerifyEmail(VerifyEmailCommand command)
        {
            await _mediator.Send(command);

            return(Ok(new { message = "Verification successful, you can now login" }));
        }
Exemple #13
0
 public HttpRequestMessage GetRequestMessage <T>(VerifyEmailCommand <T> command)
 {
     return(this.GetRequestMessage <T>(this.GetRequestUri <T>(), this.GetHttpContent(command), HttpMethod));
 }
        public async Task <IActionResult> VerifyEmailAsync([FromBody] VerifyEmailCommand model)
        {
            await Mediator.Send(model);

            return(NoContent());
        }
Exemple #15
0
 public async Task <IActionResult> VerifyEmail([CustomizeValidator(Skip = true)] VerifyEmailCommand command)
 {
     return(Ok(await Mediator.Send(command)));
     // return Ok(new object());
 }
Exemple #16
0
 public VerifyEmailHttpApiCommand(VerifyEmailCommand <T> command, IRequestMessageBuilderFactory requestMessageBuilderFactory)
 {
     this.command        = command;
     this.requestBuilder = requestMessageBuilderFactory.GetRequestMessageBuilder <VerifyEmailRequestMessageBuilder>();
 }
        public ActionResult VerifyEmail(string email, string verificationCode)
        {
            var verifyEmailCommand = new VerifyEmailCommand(email, verificationCode);

            ExecuteCommand(verifyEmailCommand);

            return View(verifyEmailCommand.Result);
        }