Ejemplo n.º 1
0
        public ActionResult Login(USUARIO uSUARIO)
        {
            IEnumerable <USUARIO> userList;
            HttpResponseMessage   response = GlobalVariables.WebApiClient.GetAsync("users").Result;

            if (response.IsSuccessStatusCode)
            {
                userList = response.Content.ReadAsAsync <IEnumerable <USUARIO> >().Result;
                var userDetails = userList.Where(x => x.EMAIL == uSUARIO.EMAIL && x.SENHA == GlobalVariables.CalculateMD5Hash(uSUARIO.SENHA)).FirstOrDefault();

                if (userDetails == null)
                {
                    TempData["Error"] = "Email ou senha inválidos.";
                    return(View(uSUARIO));
                }
                else if (userDetails.ATIVO == 0)
                {
                    TempData["Error"] = "Usuário não está ativo.";
                    return(View(uSUARIO));
                }
                else
                {
                    var identity = new ClaimsIdentity(new[]
                    {
                        new Claim(ClaimTypes.Email, userDetails.EMAIL),
                        new Claim(ClaimTypes.GivenName, userDetails.NOME),
                        new Claim(ClaimTypes.HomePhone, userDetails.CELULAR),
                        new Claim(ClaimTypes.Role, userDetails.COD_PERFIL.ToString()),
                        new Claim(ClaimTypes.Sid, userDetails.COD_USUARIO.ToString())
                    }, "ApplicationCookie");

                    var context     = Request.GetOwinContext();
                    var authManager = context.Authentication;
                    authManager.SignIn(identity);

                    if (identity.Claims.Any(c => c.Type == ClaimTypes.Role && (c.Value == "4")))
                    {
                        return(RedirectToAction("Index", "Admin"));
                    }
                    else if (identity.Claims.Any(c => c.Type == ClaimTypes.Role && (c.Value == "3")))
                    {
                        return(RedirectToAction("Index", "Master"));
                    }
                    else if (identity.Claims.Any(c => c.Type == ClaimTypes.Role && (c.Value == "5")))
                    {
                        return(RedirectToAction("Index", "Assistant"));
                    }
                    else
                    {
                        return(Redirect("/"));
                    }
                }
            }
            else
            {
                TempData["Error"] = "Operação ilegal.";
                return(View());
            }
        }