예제 #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            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(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    //_logger.LogInformation("User logged in.");
                    //return LocalRedirect(returnUrl);
                    IdentityUser identityUser = await _userManager.FindByEmailAsync(Input.Email);

                    if (!await _userManager.IsInRoleAsync(identityUser, "Admin") &&
                        !await _userManager.IsInRoleAsync(identityUser, "Membro") &&
                        !await _userManager.IsInRoleAsync(identityUser, "Participante") &&
                        !await _userManager.IsInRoleAsync(identityUser, "Director"))
                    {
                        return(RedirectToAction("Index", "Register", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        HttpContext.Session.SetString("_UserEmail", identityUser.Email);
                        if (await _userManager.IsInRoleAsync(identityUser, "Membro"))
                        {
                            var id = _membroRepository.ObterPorEmail(identityUser.Email).MembroId;
                            HttpContext.Session.SetString("_Membro", id.ToString());
                            if (await _userManager.IsInRoleAsync(identityUser, "Organizador"))
                            {
                                HttpContext.Session.SetString("_Organizador", id.ToString());
                            }
                            if (await _userManager.IsInRoleAsync(identityUser, "Cientifico"))
                            {
                                HttpContext.Session.SetString("_Cientifico", id.ToString());
                            }
                        }
                        else if (await _userManager.IsInRoleAsync(identityUser, "Participante"))
                        {
                            var id = _participanteRepository.ObterPorEmail(identityUser.Email).ParticipanteId;
                            HttpContext.Session.SetString("_Participante", id.ToString());
                            return(RedirectToAction("PIndex", "Home"));
                        }
                        else if (await _userManager.IsInRoleAsync(identityUser, "Admin"))
                        {
                            HttpContext.Session.SetString("_Admin", "Admin");
                        }
                        else if (await _userManager.IsInRoleAsync(identityUser, "Director"))
                        {
                            HttpContext.Session.SetString("_Director", "Director");
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("Conta de usuário bloqueada.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Tentativa de login inválida.");
                    return(Page());
                }
            }

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