Ejemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReCaptchaKey"] = _reCaptcha.Key;
            ViewData["ReturnUrl"]    = returnUrl;
            if (ModelState.IsValid)
            {
                Request.Form.TryGetValue("g-recaptcha-response", out var captcha);
                var captchaResult = await _reCaptcha.Verify(Request, captcha);

                if (!captchaResult.Success)
                {
                    var code = captchaResult.ErrorCodes.FirstOrDefault() ?? "";

                    var message =
                        code == "missing-input-response" ? "Please verify you are not a robot."
                        : "Something went wrong with the ReCaptcha.";

                    ModelState.AddModelError("ReCaptcha", message);

                    return(View(model));
                }

                var user   = new ApplicationUser(model.Email, model.Email);
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

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