예제 #1
0
 public ActionResult LoginButton(string id, string senha)
 {
     if (ModelState.IsValid)
     {
         if (id == "admin")
         {
             using (MD5 md5Hash = MD5.Create())
             {
                 if (MyHelpers.VerifyMd5Hash(md5Hash, senha, "21232f297a57a5a743894a0e4a801fc3"))
                 {
                     HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(id, "admin", 30);
                     Response.Cookies.Add(cookie);
                 }
                 else
                 {
                     ModelState.AddModelError("password", "Password incorreta!");
                     return(View("Login"));
                 }
             }
         }
         else
         {
             ModelState.AddModelError("", "Dados incorretos. Tente novamente.");
             return(View("Login"));
         }
     }
     else
     {
         ModelState.AddModelError("", "Invalid Request");
         return(View("Login"));
     }
     return(RedirectToAction("loginSucess", "Admin"));
 }
 public ActionResult Login(string email, string password)
 {
     if (ModelState.IsValid)
     {
         var clientes = (from c in db.Clientes
                         where c.email == email
                         select c);
         if (clientes.ToList <Cliente>().Count > 0)
         {
             Cliente cliente = clientes.ToList <Cliente>().ElementAt <Cliente>(0);
             using (MD5 md5Hash = MD5.Create())
             {
                 if (MyHelpers.VerifyMd5Hash(md5Hash, password, cliente.password))
                 {
                     HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(cliente.nif_cliente.ToString(), "user", 30);
                     Response.Cookies.Add(cookie);
                 }
                 else
                 {
                     ModelState.AddModelError("password", "Password incorreta!");
                     return(View("Index"));
                 }
             }
         }
         else
         {
             ModelState.AddModelError("", "Dados incorretos. Tente novamente.");
             return(View("Index"));
         }
     }
     else
     {
         ModelState.AddModelError("", "Invalid Request");
         return(View("Index"));
     }
     return(RedirectToAction("loginSucess", "Login"));
 }
 public ActionResult LoginButton(int id, string password)
 {
     if (ModelState.IsValid)
     {
         var funcionarios = (from f in db.Funcionarios
                             where f.id_funcionario == id
                             select f);
         if (funcionarios.ToList <Funcionario>().Count > 0)
         {
             Funcionario funcionario = funcionarios.ToList <Funcionario>().ElementAt <Funcionario>(0);
             using (MD5 md5Hash = MD5.Create())
             {
                 if (MyHelpers.VerifyMd5Hash(md5Hash, password, funcionario.password))
                 {
                     HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(funcionario.id_funcionario.ToString(), "staff", 10);
                     Response.Cookies.Add(cookie);
                 }
                 else
                 {
                     ModelState.AddModelError("password", "Password incorreta!");
                     return(View("Index"));
                 }
             }
         }
         else
         {
             ModelState.AddModelError("", "Dados incorretos. Tente novamente.");
             return(View("Index"));
         }
     }
     else
     {
         ModelState.AddModelError("", "Invalid Request");
         return(View("Index"));
     }
     return(RedirectToAction("loginSucess", "Mobile"));
 }
예제 #4
0
        public ActionResult Login(string username, string password)
        {
            DefaultController.Funcionario           = new Funcionario();
            DefaultController.Funcionario.Pass_word = password;
            int userName = Int32.Parse(username);

            if (ModelState.IsValid)
            {
                var userC = (from m in db.Cliente where (m.Id == userName) select m);

                if (userC.ToList().Count > 0)
                {
                    Cliente cliente = userC.ToList().ElementAt <Cliente>(0);
                    using (MD5 md5Hash = MD5.Create())
                    {
                        if (MyHelpers.VerifyMd5Hash(md5Hash, password, cliente.Pass_word))
                        {
                            string     client = cliente.Id.ToString();
                            HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(cliente.Id.ToString(), cliente.Role);
                            Response.Cookies.Add(cookie);
                            ViewData["User_Name"] = "Bem vindo" + cliente.Nome;
                            return(RedirectToAction("Index", "Cliente"));
                        }
                        else
                        {
                            ModelState.AddModelError("password", "Password incorreta!");
                            return(View());
                        }
                    }
                }
                else
                {
                    var userF = (from m in db.Funcionario where (m.Id == userName) select m);
                    if (userF.ToList().Count > 0)
                    {
                        Funcionario funcionario = userF.ToList().ElementAt <Funcionario>(0);
                        using (MD5 md5Hash = MD5.Create())
                        {
                            if (MyHelpers.VerifyMd5Hash(md5Hash, password, funcionario.Pass_word))
                            {
                                string     func   = funcionario.Id.ToString();
                                HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(funcionario.Id.ToString(), funcionario.Role);
                                Response.Cookies.Add(cookie);
                                if (funcionario.Role.Equals("func"))
                                {
                                    ViewData["User_Name"] = "Bem vindo" + funcionario.Nome;
                                    return(RedirectToAction("Index", "Funcionario"));
                                }
                                else
                                {
                                    var t = ViewData["User_Name"] = "Bem vindo" + funcionario.Nome;
                                    return(RedirectToAction("Index", "Admin"));
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("password", "Password incorreta!");
                                return(View());
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Login data is incorrect!");
                        return(View());
                    }
                }
            }
            return(View());
        }
예제 #5
0
        public ActionResult Autentica(string email, string password)
        {
            string pattern = @"^[eap][0-9]{3}$";
            Regex  rgx     = new Regex(pattern, RegexOptions.IgnoreCase);

            if (ModelState.IsValid)
            {
                if (rgx.IsMatch(email))
                {
                    var funcs = (from m in db.Funcionarios
                                 where m.idFunc == email && m.estadoConta != "desativado"
                                 select m);
                    if (funcs.ToList <Funcionario>().Count > 0)
                    {
                        Funcionario func = funcs.ToList <Funcionario>().ElementAt <Funcionario>(0);
                        using (MD5 md5Hash = MD5.Create())
                        {
                            if (MyHelpers.VerifyMd5Hash(md5Hash, password, func.password))
                            {
                                FormsAuthentication.SetAuthCookie(func.idFunc.ToString(), false);
                            }
                            else
                            {
                                ModelState.AddModelError("password", "Password incorreta!");
                                return(View("Index"));
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Login data is incorrect!");
                        return(View("Index"));
                    }
                }
                else
                {
                    var clientes = (from m in db.Clientes
                                    where m.email == email && m.estadoConta != "desativado"
                                    select m);
                    if (clientes.ToList <Cliente>().Count > 0)
                    {
                        Cliente cliente = clientes.ToList <Cliente>().ElementAt <Cliente>(0);
                        using (MD5 md5Hash = MD5.Create())
                        {
                            if (MyHelpers.VerifyMd5Hash(md5Hash, password, cliente.password))
                            {
                                FormsAuthentication.SetAuthCookie(cliente.email, false);
                                Session["Carrinho"] = new List <Tuple <int, String, float, int> >(); //id, designacao, preco, quantidade
                                Session["Total"]    = 0.0f;
                            }
                            else
                            {
                                ModelState.AddModelError("password", "Password incorreta!");
                                return(View("Index"));
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Login data is incorrect!");
                        return(View("Index"));
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid Request");
                return(View("Index"));
            }
            if (rgx.IsMatch(email))                     // É um funcionário
            {
                if (email[0] == 'A' || email[0] == 'a') // Administrador
                {
                    return(RedirectToAction("IndexProduto", "Manutencao"));
                }
                if (email[0] == 'E' || email[0] == 'e') // Estafeta
                {
                    return(RedirectToAction("Percurso", "Realizacao"));
                }
                if (email[0] ==
                    'P' || email[0] == 'p')                     // Padeiro
                {
                    return(RedirectToAction("IndexProducao", "Pesquisa"));
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
예제 #6
0
        public ActionResult Login(string Username, string Password)
        {
            if (ModelState.IsValid)
            {
                var clientes = (from m in db.Clientes
                                where m.Username == Username
                                select m);

                if (clientes.ToList <Cliente>().Count == 0)
                {
                    var funcionarios = (from m in db.Funcionarios
                                        where m.Username == Username
                                        select m);

                    if (funcionarios.ToList <Funcionario>().Count > 0)
                    {
                        Funcionario funcionario = funcionarios.ToList <Funcionario>().ElementAt <Funcionario>(0);
                        using (MD5 md5Hash = MD5.Create())
                        {
                            if (MyHelpers.VerifyMd5Hash(md5Hash, Password, funcionario.Password))
                            {
                                HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(funcionario.Username, funcionario.Role);
                                Response.Cookies.Add(cookie);
                                if (Username.Equals("admin"))
                                {
                                    return(RedirectToAction("Index", "Admin"));
                                }
                                else
                                {
                                    return(RedirectToAction("Index", "Funcionario"));
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("password", "Password incorreta!");
                                return(View("Index"));
                            }
                        }
                    }
                }
                else
                {
                    Cliente cliente = clientes.ToList <Cliente>().ElementAt <Cliente>(0);
                    using (MD5 md5Hash = MD5.Create())
                    {
                        if (MyHelpers.VerifyMd5Hash(md5Hash, Password, cliente.Password))
                        {
                            HttpCookie cookie = MyHelpers.CreateAuthorizeTicket(cliente.Username, cliente.Role);
                            Response.Cookies.Add(cookie);
                            return(RedirectToAction("Index", "Cliente"));
                        }
                        else
                        {
                            ModelState.AddModelError("password", "Password incorreta!");
                            return(View("Index"));
                        }
                    }
                }
            }
            ModelState.AddModelError("", "Username incorreto!");
            return(View("Index"));
        }
예제 #7
0
        public async System.Threading.Tasks.Task <ActionResult> LoginAsync(string email, string password)
        {
            DefaultController.Utilizador          = new Utilizador();
            DefaultController.Utilizador.Password = password;
            // int userName = Int32.Parse(username);
            if (ModelState.IsValid)
            {
                var userS = (from u
                             in model.Utilizador
                             where (u.Email == email && u.Tipo == "single")
                             select u);

                if (userS.ToList().Count > 0)
                {
                    Utilizador userSingle = userS.ToList().ElementAt <Utilizador>(0);
                    if (userSingle.Estado != 2)
                    {
                        using (MD5 md5Hash = MD5.Create())
                        {
                            if (MyHelpers.VerifyMd5Hash(md5Hash, password, userSingle.Password))
                            {
                                var claims = new List <Claim>
                                {
                                    new Claim(ClaimTypes.Name, email),
                                    new Claim(ClaimTypes.Role, "User")
                                };

                                var             identidadeDeUsuario = new ClaimsIdentity(claims, "Login");
                                ClaimsPrincipal claimPrincipal      = new ClaimsPrincipal(identidadeDeUsuario);

                                var propriedadesDeAutenticacao = new AuthenticationProperties
                                {
                                    AllowRefresh = true,
                                    ExpiresUtc   = DateTime.Now.ToLocalTime().AddHours(10),
                                    IsPersistent = true
                                };
                                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimPrincipal, propriedadesDeAutenticacao);

                                Helpers.CacheController.utilizador = userSingle.Email;
                                return(RedirectToAction("Index", "Utilizador"));
                            }
                            else

                            {
                                //ViewData["User_Name"] = "Bem vindo" + userSingle.Nome;
                                ModelState.AddModelError("", "E-mail ou password incorreto(s).");
                                return(View());
                            }
                        }
                    }
                }

                var administrador = (from a
                                     in model.Administrador
                                     where (a.Email == email && a.Password == password)
                                     select a);

                if (administrador.ToList().Count > 0)
                {
                    Administrador admin = administrador.ToList().ElementAt <Administrador>(0);


                    Helpers.CacheController.utilizador = admin.Email;
                    return(RedirectToAction("Index", "Admin"));
                }

                var userC = (from m in model.Utilizador where (m.Email == email && m.Tipo == "company") select m);
                if (userC.ToList().Count > 0)
                {
                    Utilizador utilizador = userC.ToList().ElementAt <Utilizador>(0);
                    using (MD5 md5Hash = MD5.Create())
                    {
                        if (MyHelpers.VerifyMd5Hash(md5Hash, password, utilizador.Password))
                        {
                            Helpers.CacheController.utilizador = utilizador.Email;

                            return(RedirectToAction("Index", "Company"));
                        }
                        else
                        {
                            ModelState.AddModelError("password", "Password incorreta!");
                            return(View());
                        }
                    }
                }
            }
            {
                //ViewData["User_Name"] = "Bem vindo" + userSingle.Nome;
                ModelState.AddModelError("", "E-mail ou password incorreto(s).");
                return(View());
            }
            return(View());
        }