Beispiel #1
0
        public async Task <IActionResult> Login(string returnUrl)
        {
            SignIn_vmodel model = new SignIn_vmodel()
            {
                ReturnUrl      = returnUrl,
                ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

            return(View(model));
        }
Beispiel #2
0
        public async Task <IActionResult> Login(SignIn_vmodel model, string returnUrl)
        {
            model.ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (ModelState.IsValid)
            {
                App_IdentityUser user;

                if (model.UserOrEmail.Contains('@'))
                {
                    user = await userManager.FindByEmailAsync(model.UserOrEmail);
                }
                else
                {
                    user = await userManager.FindByNameAsync(model.UserOrEmail);
                }

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

                    var result = await signInManager.PasswordSignInAsync(user, model.Password,
                                                                         model.PersistentCookies, true);

                    if (result.Succeeded && (await userManager.GetClaimsAsync(user)).Count != 0)
                    {
                        if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                        {
                            return(LocalRedirect(returnUrl));
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                    else if (result.Succeeded)
                    {
                        return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed }));
                    }
                    else if (result.IsLockedOut)
                    {
                        return(View("AccountLocked"));
                    }
                }
                ModelState.AddModelError(string.Empty, " Intento invalido de inicio de sesión");
            }
            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 }));
            }
        }