public async Task <ActionResult <Result> > Get([FromForm] string code, [FromForm] string pass, [FromForm] string captcha = "")
        {
            try
            {
                if (string.IsNullOrEmpty(code))
                {
                    throw new ArgumentException(localizer[Controllers_ResultController.Visitor_code_must_not_be_empty].Value);
                }

                if (string.IsNullOrEmpty(pass))
                {
                    throw new ArgumentException(localizer[Controllers_ResultController.Last_4_digits_of_personal_number_or_declared_passport_for_foreigner_at_registration_must_not_be_empty].Value);
                }

                if (!string.IsNullOrEmpty(configuration["googleReCaptcha:SiteKey"]))
                {
                    if (string.IsNullOrEmpty(captcha))
                    {
                        throw new Exception("Please provide captcha");
                    }

                    var validation = await captchaValidator.IsCaptchaPassedAsync(captcha);

                    if (!validation)
                    {
                        throw new Exception("Please provide valid captcha");
                    }
                }

                var codeClear = FormatBarCode(code);
                if (int.TryParse(codeClear, out var codeInt))
                {
                    return(Ok(await visitorRepository.GetTest(codeInt, pass)));
                }
                throw new Exception(localizer[Controllers_ResultController.Invalid_visitor_code]);
            }
            catch (ArgumentException exc)
            {
                logger.LogError(exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (!await _captchaValidator.IsCaptchaPassedAsync(Input.Captcha))
            {
                ModelState.AddModelError("captcha", _localizer["Captcha validation failed"]);
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = Input.Username, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    return(RedirectToPage("/Login"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(Page());
        }
Example #3
0
        public async Task <ActionResult> Login(LoginViewModel model, string captcha)
        {
            if (!await captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("", "Captcha validation failed");
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string password = string.Empty;

            using (MD5 md5Hash = MD5.Create())
            {
                password = GetMd5Hash(md5Hash, model.Password);
            }

            var user = userService.GetByEmailAndPassword(model.Email, password);

            if (user != null)
            {
                await SignIn(user);

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                ModelState.AddModelError("", "Invalid session");
                return(View(model));
            }
        }
Example #4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (!await _captchaValidator.IsCaptchaPassedAsync(Input.Captcha))
            {
                ModelState.AddModelError("captcha", _localizer["Captcha validation failed"]);
            }
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByEmailAsync(Input.Email);

                if (user != null)
                {
                    var result = await _signInManager.PasswordSignInAsync(user, Input.Password, Input.RememberMe, false);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User logged in.");
                        return(LocalRedirect(returnUrl));
                    }
                }
            }

            ModelState.AddModelError(string.Empty, _localizer["Invalid login attempt"]);

            return(Page());
        }
Example #5
0
        public async Task <IActionResult> OnPostAsync(string captcha)
        {
            _logger.LogDebug("ContactMe.OnPostSync entered");

            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");
                return(Page());
            }
            if (!ModelState.IsValid)
            {
                _logger.LogDebug("Model state not valid");
                return(Page());
            }

            string mailText = EmailHelper.BuildTemplate(_templatesPath, "ContactMeTemplate.html");

            mailText = mailText.Replace("[fromEmail]", Input.Email).Replace("[fromName]", Input.Name).Replace("[contactmessage]", Input.Message);

            await _emailService.SendAsync("*****@*****.**", Input.Subject, mailText, true);

            // On success go to contact result result which is a thankyou page
            _logger.LogDebug("Email sent");
            return(RedirectToPage("ContactResult"));
        }
    public async Task <IActionResult> OnPostAsync(string captcha, string inviteCode, string email, string?returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");
        if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
        {
            ModelState.AddModelError("captcha", "Captcha validation failed");
        }
        if (ModelState.IsValid)
        {
            if (Input is null)
            {
                throw new Exception("Input is null.");
            }
            var user = new ApplicationUser {
                UserName = email, Email = email
            };
            var result = await _userManager.CreateAsync(user, Input.Password);

            if (result.Succeeded)
            {
                _logger.LogInformation("User created a new account with password.");

                var newUserEvent = new NewUserRegisteredEvent(email,
                                                              Request.HttpContext.Connection.RemoteIpAddress !.ToString());

                await _dispatcher.Dispatch(newUserEvent);

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var userId = user.Id;

                var emailConfirmationResult = await _userManager.ConfirmEmailAsync(user, code);

                if (!emailConfirmationResult.Succeeded)
                {
                    throw new InvalidOperationException($"Error confirming email for user with ID '{userId}':");
                }

                await _newMemberService.MemberSetupAsync(userId, Input.FirstName !, Input.LastName !, inviteCode !, email);

                _logger.LogInformation($"Adding user {user.Email} to Member Role");
                var roles = await _roleManager.Roles.ToListAsync();

                var memberRole = roles.FirstOrDefault(r => r.Name == "Member");
                if (memberRole != null)
                {
                    await _userRoleMembershipService.AddUserToRoleAsync(userId, memberRole.Id);
                }
                return(RedirectToRoute("/User/MyProfile"));
            }
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }

        // If we got this far, something failed, redisplay form
        return(Page());
    }
Example #7
0
 public async Task <IActionResult> Register(RegisterViewModel model, string token)
 {
     if (!await _captchaValidator.IsCaptchaPassedAsync(token))
     {
         ModelState.AddModelError("captcha", "Captcha validation failed");
     }
     return(View());
 }
Example #8
0
    public async Task <IActionResult> OnPostAsync(string captcha, string?returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");
        if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
        {
            ModelState.AddModelError("captcha", "Captcha validation failed");
        }
        if (ModelState.IsValid)
        {
            if (Input is null)
            {
                throw new Exception("Input is null.");
            }
            var user = new ApplicationUser {
                UserName = Input.Email, Email = Input.Email
            };
            var result = await _userManager.CreateAsync(user, Input.Password);

            if (result.Succeeded)
            {
                _logger.LogInformation("User created a new account with password.");

                var newUserEvent = new NewUserRegisteredEvent(Input.Email !,
                                                              Request.HttpContext.Connection.RemoteIpAddress !.ToString());

                await _dispatcher.Dispatch(newUserEvent);

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var callbackUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { userId = user.Id, code = code },
                    protocol: Request.Scheme);
                if (string.IsNullOrEmpty(callbackUrl))
                {
                    throw new Exception("Callback URL is null or empty.");
                }

                if (string.IsNullOrEmpty(Input.Email))
                {
                    throw new Exception("Email is required.");
                }
                await _emailService.SendEmailAsync(Input.Email, "Confirm your email",
                                                   $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                return(LocalRedirect("~/Identity/Account/EmailVerificationRequired"));
            }
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }

        // If we got this far, something failed, redisplay form
        return(Page());
    }
 public async Task <IActionResult> Index(SampleViewModel collection, string captcha)
 {
     if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
     {
         ModelState.AddModelError("captcha", "Captcha validation failed");
     }
     if (ModelState.IsValid)
     {
         ViewData["Message"] = "Success";
     }
     return(View(collection));
 }
Example #10
0
        public async Task <IActionResult> OnPostAsync(string captcha, string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");
            }

            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.UserName, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Example #11
0
        public async Task <IActionResult> OnPostAsync(int?categoryId, string captcha, string returnUrl = null)
        {
            ProductIndex = _productVMService.GetProductsVM(HttpContext, null);

            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");
                return(Page());
            }

            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");

                    var user = await _userManager.FindByEmailAsync(Input.Email);

                    if (user != null)
                    {
                        await _loginCartManagerService.ManageCart(HttpContext, user.Id);
                    }

                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Example #12
0
        public async Task <IActionResult> BookTicket(string captcha)
        {
            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");

                return(View(new BookResult()
                {
                    IsSuccess = false, Message = "Captcha validation failed"
                }));
            }
            else
            {
                return(RedirectToAction("Result", _service.Add(new Ticket())));
            }
        }
Example #13
0
        public async Task <IActionResult> Contact(string nameuser, string mailto, string subjest, string message, string captcha)
        {
            MailTextClass mailText = new MailTextClass()
            {
                NameUser = nameuser, MailTo = mailto, Subjest = subjest, Message = message
            };

            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");
            }
            if (ModelState.IsValid)
            {
                ViewData["Message"] = "Сообщение отправленно.";
            }
            return(View(mailText));
        }
Example #14
0
        public async Task <IActionResult> CreateComment(int PostId, Comment comment, string captcha)
        {
            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");
            }
            if (ModelState.IsValid)
            {
                _context.Add(comment);
                await _context.SaveChangesAsync();

                ViewData["Message"] = "Success";

                return(View("Post", new Comment {
                    PostId = comment.PostId
                }));
            }
            return(RedirectToAction("Post", new { id = comment.PostId }));
        }
        public async Task <IActionResult> OnPostAsync(string captcha, string returnUrl = null)
        {
            System.Threading.Thread.Sleep(2000);

            if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
            {
                ModelState.AddModelError("captcha", "Captcha validation failed");
                return(Page());
            }

            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }