Ejemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var registerResult = await _userManager.CreateAsync(user, model.Password);

                if (registerResult.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit https://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 : true);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(new CustomJsonResult(new { model.Email }));
                }
                AddErrors(registerResult);
            }

            // If we got this far, something failed, redisplay form
            return(CustomJsonResult.Errors(ModelState.GetErrors()));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login(LoginModel model, string returnUrl = null)
        {
            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(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation(1, "User logged in.");
                    return(new CustomJsonResult(new { model.Email }));
                }
                if (result.RequiresTwoFactor)
                {
                    return(new CustomJsonResult(new {
                        sendCode = nameof(SendCode),
                        ReturnUrl = returnUrl,
                        RememberMe = model.RememberMe
                    }));
                }
                if (result.IsLockedOut)
                {
                    ModelState.AddModelError(string.Empty, "Locked out.");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                }
            }

            // If we got this far, something failed, redisplay form
            return(CustomJsonResult.Errors(ModelState.GetErrors()));
        }