Esempio n. 1
0
        public async Task <IActionResult> Login(UserModel model)
        {
            AuthValidatior authValidation      = new AuthValidatior(userManager, resManager);
            Dictionary <string, string> errors = authValidation.LoginCheck(model);

            if (!errors.Any())
            {
                User user = await userManager.FindByNameAsync(model.UserName);

                if (user != null)
                {
                    if (user.EmailConfirmed)
                    {
                        await signInManager.SignOutAsync();

                        SignInResult signInResult = await signInManager.PasswordSignInAsync(user, model.Password, true, true);

                        if (signInResult.Succeeded)
                        {
                            return(Json(Url.Action("Index", "Home")));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("ConfirmYourEmail", resManager.GetString("ConfirmYourEmail"));
                    }
                }
            }
            errors.ToList().ForEach(error => ModelState.AddModelError(error.Key, error.Value));
            return(BadRequest(ModelState));
        }
Esempio n. 2
0
        public async Task <IActionResult> ResetPassword(UserModel model)
        {
            AuthValidatior authValidation      = new AuthValidatior(userManager, resManager);
            Dictionary <string, string> errors = authValidation.ResetPasswordCheck(model);

            if (!errors.Any())
            {
                User user = await userManager.FindByNameAsync(model.UserName);

                if (user != null)
                {
                    await userManager.ChangePasswordAsync(user, model.OldPassword, model.Password);

                    return(RedirectToAction("ResetPasswordSuccess"));
                }
            }
            errors.ToList().ForEach(error => ModelState.AddModelError(error.Key, error.Value));
            return(View("ResetPassword", ModelState));
        }
Esempio n. 3
0
        public async Task <IActionResult> ResetPasswordSendEmail(UserModel model)
        {
            AuthValidatior authValidation      = new AuthValidatior(userManager, resManager);
            Dictionary <string, string> errors = authValidation.SpecifyEmailCheck(model);

            if (!errors.Any())
            {
                User user = await userManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    EmailSender emailSender      = new EmailSender(configuration, resManager);
                    string      resetPasswordUrl = Url.Action("ResetPassword", AUTH, new { }, HttpContext.Request.Scheme);
                    await emailSender.SendResetPasswordEmail(model, resetPasswordUrl);

                    return(RedirectToAction("ResetPasswordMessage", AUTH));
                }
            }
            errors.ToList().ForEach(error => ModelState.AddModelError(error.Key, error.Value));
            return(View("ResetPasswordSpecifyEmail", ModelState));
        }
Esempio n. 4
0
        public async Task <IActionResult> Signup(UserModel model)
        {
            AuthValidatior authValidation      = new AuthValidatior(userManager, resManager);
            Dictionary <string, string> errors = authValidation.RegistrationCheck(model);

            if (!errors.Any())
            {
                User user = new User()
                {
                    UserName       = model.UserName,
                    Email          = model.Email,
                    EmailConfirmed = false
                };
                IdentityResult identityResult = await userManager.CreateAsync(user, model.Password);

                if (identityResult.Succeeded)
                {
                    string token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    string      confirmEmailUrl = Url.Action("ConfirmEmail", AUTH, new { userId = user.Id, token }, protocol: HttpContext.Request.Scheme);
                    EmailSender emailSender     = new EmailSender(configuration, resManager);
                    await emailSender.SendRegisterConfirmationEmail(model, confirmEmailUrl);

                    return(Json(Url.Action("ConfirmEmailMessage", AUTH)));
                }
                else
                {
                    foreach (IdentityError error in identityResult.Errors)
                    {
                        ModelState.AddModelError(error.Code, error.Description);
                    }
                }
            }
            errors.ToList().ForEach(error => ModelState.AddModelError(error.Key, error.Value));
            return(BadRequest(ModelState));
        }