Пример #1
0
        public ActionResult Index(ValidateLogin login, FormCollection form)
        {
            ViewData["Result"] = "";
            if (ModelState.IsValid)
            {
                RepositoryOBTRS          mOBTRS  = new RepositoryOBTRS();
                List <tbl_Administrator> LogName = mOBTRS.Login(form.Get("email").Trim(), form.Get("password").Trim());
                if (LogName == null)
                {
                    //Khong the ket noi Database
                    ViewData["Result"] = "Can't connect to database server, please try again later!";
                    return(View(login));
                }
                else if (LogName.Count == 1)
                {
                    if (LogName[0].Admin_Active != false)
                    {
                        if (form.Get("Remember").ToString().ToLower() != "false")
                        {
                            //Luu vao Cookie
                            //HttpCookie cookie = HttpContext.Response.Cookies.Get("cmsOBTRS");
                            HttpCookie cookie = new HttpCookie("cmsOBTRS");
                            cookie.Values["Username"] = LogName[0].Admin_Name;
                            cookie.Values["ID"]       = LogName[0].Admin_ID.ToString();
                            cookie.Expires            = DateTime.Now.AddDays(+14);
                            HttpContext.Response.Cookies.Set(cookie);
                        }

                        //Luu vào Session
                        Session["LogedName"] = LogName[0].Admin_Name;
                        Session["Admin_ID"]  = LogName[0].Admin_ID;

                        //Lay Alias Role cua Admin dang nhap he thong
                        Session["Role"] = mOBTRS.CheckRole(LogName[0].Admin_RoleID);
                        string a = Session["Role"].ToString();

                        //Dang nhap thanh cong
                        if (!Response.IsRequestBeingRedirected)
                        {
                            Response.Redirect("/CMS/WelcomeAptech");
                        }
                    }
                    else
                    {
                        //Bi cam truy cap
                        ViewData["Result"] = "Access Denied!";
                        return(View(login));
                    }
                }
                else
                {
                    //Login sai mat khau va ten dang nhap
                    ViewData["Result"] = "Username or password invalid.Login failure!";
                    return(View(login));
                }
            }

            //Login unsuccessful
            return(View(login));
        }