Exemplo n.º 1
0
        private int IsValid(UserLoginModels user)
        {
            int flag = 0;

            using (JShopVNEntities db1 = new JShopVNEntities())
            {
                var uLogin = new Account();
                if (db1.Accounts
                    .Where(b => b.username.Equals(user.username) && b.password.Equals(user.password))
                    .FirstOrDefault() != null)
                {
                    uLogin = db1.Accounts.Where(b => b.username.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault();
                }

                if (uLogin != null)
                {
                    if (uLogin.roleid == 1)
                    {
                        flag = 1;
                    }
                    else if (uLogin.roleid == 2)
                    {
                        flag = 2;
                    }
                }
                else
                {
                    flag = 0;
                }
            }
            return(flag);
        }
Exemplo n.º 2
0
        public ActionResult LoginA(UserLoginModels user)
        {
            if (IsValid(user))
            {
                using (Entities1 db1 = new Entities1())
                {
                    List <User> listU      = db1.Users.ToList();
                    bool        isUsername = false;
                    bool        isPhone    = false;
                    bool        isEmail    = false;
                    foreach (User us in listU)
                    {
                        if (us.username != null && us.username.Equals(user.username))
                        {
                            isUsername = true;
                        }
                        if (us.phone != null && us.phone.Equals(user.username))
                        {
                            isPhone = true;
                        }
                        if (us.email != null && us.email.Equals(user.username))
                        {
                            isEmail = true;
                        }
                    }
                    if (isUsername == true && isPhone == false && isEmail == false)
                    {
                        FormsAuthentication.SetAuthCookie(user.username, false);
                        Session["user"]     = db1.Users.Where(b => b.username.Equals(user.username)).FirstOrDefault().fullname;
                        Session["username"] = user.username;
                    }
                    else if (isUsername == false && isPhone == true && isEmail == false)
                    {
                        FormsAuthentication.SetAuthCookie(user.username, false);
                        Session["user"]     = db1.Users.Where(b => b.phone.Equals(user.username)).FirstOrDefault().fullname;
                        Session["username"] = db1.Users.Where(b => b.phone.Equals(user.username)).FirstOrDefault().username;
                    }
                    else if (isUsername == false && isPhone == false && isEmail == true)
                    {
                        FormsAuthentication.SetAuthCookie(user.username, false);
                        Session["user"]     = db1.Users.Where(b => b.email.Equals(user.username)).FirstOrDefault().fullname;
                        Session["username"] = db1.Users.Where(b => b.email.Equals(user.username)).FirstOrDefault().username;
                    }
                    else if (isUsername == false && isPhone == false && isEmail == false)
                    {
                        ViewBag.ErrorMessege = "<script>alert('Sai username hoặc password');</script>";
                        return(View());
                    }
                }


                return(RedirectToAction("Index", "Products"));
            }
            else
            {
                ViewBag.ErrorMessege = "<script>alert('Không có user');</script>";
                return(View(user));
            }
        }
Exemplo n.º 3
0
        public void IsValidTest4()
        {
            var user = new UserLoginModels()
            {
                username = "******",
                password = "******"
            };

            Assert.AreEqual(false, loginController.Login(user, String.Empty));
        }
Exemplo n.º 4
0
        public void IsValidTest3()
        {
            var user = new UserLoginModels()
            {
                username = "******",
                password = "******"
            };

            Assert.AreEqual(false, loginController.IsValid(user));
        }
Exemplo n.º 5
0
        public void IsValidTest2()
        {
            var user = new UserLoginModels()
            {
                username = "******",
                password = "******"
            };

            Assert.AreEqual(true, loginController.IsValid(user));
        }
        public IActionResult Register(UserLoginModels user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            userRepository.Add(new User {
                Name = user.Name, Password = user.Password
            });
            return(Ok());
        }
Exemplo n.º 7
0
 public ActionResult LoginA(UserLoginModels user, string returnURL)
 {
     if (ModelState.IsValid)
     {
         if (IsValid(user) == 2)
         {
             FormsAuthentication.SetAuthCookie(user.username, false);
             Session["username"] = user.username;
             return(RedirectToAction("Index", "Customer"));
         }
         else
         {
             ViewBag.ErrorMessege = "<script>alert('Sai tài khoản hoặc mật khẩu');</script>";
             return(View(user));
         }
     }
     else
     {
         return(View(user));
     }
 }
        public IActionResult Login(UserLoginModels user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var authUser = userRepository.GetAll()
                           .FirstOrDefault(u => u.Name == user.Name &&
                                           u.Password == user.Password);

            if (authUser == null)
            {
                return(Unauthorized());
            }

            var claims = getClaims(authUser);
            var token  = getToken(claims);

            return(Ok(new JwtSecurityTokenHandler().WriteToken(token)));
        }
Exemplo n.º 9
0
        public bool IsValid(UserLoginModels user)
        {
            bool flag = false;

            using (Entities1 db1 = new Entities1())
            {
                List <User> listU      = db1.Users.ToList();
                var         uLogin     = new User();
                bool        isUsername = false;
                bool        isPhone    = false;
                bool        isEmail    = false;
                foreach (User us in listU)
                {
                    if (us.username != null && us.username.Equals(user.username))
                    {
                        isUsername = true;
                    }
                    if (us.phone != null && us.phone.Equals(user.username))
                    {
                        isPhone = true;
                    }
                    if (us.email != null && us.email.Equals(user.username))
                    {
                        isEmail = true;
                    }
                }
                if (isUsername == true && isPhone == false && isEmail == false)
                {
                    if (db1.Users
                        .Where(b => b.username.Equals(user.username) && b.password.Equals(user.password))
                        .FirstOrDefault() != null)
                    {
                        uLogin = db1.Users.Where(b => b.username.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault();
                    }
                }
                else if (isUsername == false && isPhone == true && isEmail == false)
                {
                    if (db1.Users.Where(b => b.phone.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault() != null)
                    {
                        uLogin = db1.Users.Where(b => b.phone.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault();
                    }
                }
                else if (isUsername == false && isPhone == false && isEmail == true)
                {
                    if (db1.Users.Where(b => b.email.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault() != null)
                    {
                        uLogin = db1.Users.Where(b => b.email.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault();
                    }
                }
                if (uLogin != null)
                {
                    if (uLogin.status == null)
                    {
                        flag = false;
                    }
                    else if (uLogin.status.Equals("Active") && uLogin.typeID == 3)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = false;
                }
            }
            return(flag);
        }