Beispiel #1
0
        public async Task <IActionResult> ForgotPassword(ForgotPassword_vmodel model)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByEmailAsync(model.Email);

                if (user != null && await userManager.IsEmailConfirmedAsync(user))
                {
                    var token = await userManager.GeneratePasswordResetTokenAsync(user);

                    var passwordResetLink = Url.Action("ResetPassword", "Account", new { email = model.Email, token }, Request.Scheme);

                    string message = HtmlEmailTemplate.CreateHtmlBody(user.UserName, "Has click para restablecer su contraseña:",
                                                                      passwordResetLink, "Verificar Correo Electrónico");

                    mailService.SendEmail(user.Email, "Restablecer su contraseña", message);

                    logger.Log(LogLevel.Information, $"Se envio el token de restablecimiento de contraseña para el usuario {user.UserName}");

                    return(View("ForgotPasswordConfirmation"));
                }
                return(View("ForgotPasswordConfirmation"));
            }
            return(View(model));
        }
Beispiel #2
0
        public async Task <IActionResult> Register(CreateAccount_vmodel model)
        {
            if (ModelState.IsValid)
            {
                if (authorizedEmailRepo.GetByEmail(model.Email) == null)
                {
                    ModelState.AddModelError(string.Empty, "Su correo NO esta autorizado para registrarse");
                    return(View(model));
                }

                var user = new App_IdentityUser()
                {
                    UserName = model.Username,
                    Email    = model.Email,
                    Gender   = model.Gender
                };

                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    var    confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token }, Request.Scheme);
                    string message          = HtmlEmailTemplate.CreateHtmlBody(user.UserName, "Has click para verificar su cuenta de correo electrónico:",
                                                                               confirmationLink, "Verificar Correo Electrónico");

                    mailService.SendEmail(user.Email, "Verificación de correo electrónico", message);

                    logger.Log(LogLevel.Information, $"Se envio el token de confirmacion de email para el usuario {user.UserName}");

                    if (signInManager.IsSignedIn(User))
                    {
                        return(RedirectToAction("ListUsers", "Administration"));
                    }

                    await signInManager.SignInAsync(user, isPersistent : false);

                    if ((await userManager.GetClaimsAsync(user)).Count() > 0)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed }));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            return(View(model));
        }
Beispiel #3
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            returnUrl ??= Url.Content("~/");

            SignIn_vmodel model = new SignIn_vmodel()
            {
                ReturnUrl      = returnUrl,
                ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                return(View("Login", model));
            }

            var info = await signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ModelState.AddModelError(string.Empty, "Error loading external login information");
                return(View("Login", model));
            }

            var email = info.Principal.FindFirstValue(ClaimTypes.Email);

            if (authorizedEmailRepo.GetByEmail(email) == null)
            {
                ModelState.AddModelError(string.Empty, "Su correo NO esta autorizado para registrarse");
                return(View("Login", model));
            }

            App_IdentityUser user;

            if (email != null)
            {
                user = await userManager.FindByEmailAsync(email);

                if (user != null && !user.EmailConfirmed)
                {
                    ModelState.AddModelError(string.Empty, "Email no confirmado aún");
                    return(View("Login", model));
                }
            }

            var signInResult = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey,
                                                                            isPersistent : false, bypassTwoFactor : true);

            if (signInResult.Succeeded)
            {
                user = await userManager.FindByEmailAsync(info.Principal.FindFirstValue(ClaimTypes.Email));

                if ((await userManager.GetClaimsAsync(user)).Count() > 0)
                {
                    return(LocalRedirect(returnUrl));
                }
                return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed }));
            }
            else
            {
                user = await userManager.FindByEmailAsync(email);

                if (user == null)
                {
                    string username = info.Principal.FindFirstValue(ClaimTypes.Name);
                    if (username.Contains(" "))
                    {
                        username = username.Split(' ')[0].ToLower();
                    }
                    else
                    {
                        username = username.ToLower();
                    }
                    user = new App_IdentityUser()
                    {
                        UserName = username,
                        Email    = info.Principal.FindFirstValue(ClaimTypes.Email)
                    };

                    var result = await userManager.CreateAsync(user);

                    if (!result.Succeeded)
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, $"{error.Description}");
                        }
                        return(View("Login", model));
                    }

                    var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token }, Request.Scheme);

                    string message = HtmlEmailTemplate.CreateHtmlBody(user.UserName, "Has click para verificar su cuenta de correo electrónico:",
                                                                      confirmationLink, "Verificar Correo Electrónico");

                    mailService.SendEmail(user.Email, "Verificación de correo electrónico", message);
                }

                await userManager.AddLoginAsync(user, info);

                await signInManager.SignInAsync(user, isPersistent : false);

                if ((await userManager.GetClaimsAsync(user)).Count() > 0)
                {
                    return(LocalRedirect(returnUrl));
                }
                return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed }));
            }
        }