Esempio n. 1
0
        public ActionResult Search(string Name, string Cate, int?page)
        {
            string redirectURL = string.Empty;
            WCFBagServiceClient bagServiceClient = new WCFBagServiceClient();

            Bag[] listBags = bagServiceClient.GetAllBooksForAdmin();
            if (!String.IsNullOrEmpty(Name))
            {
                listBags = listBags.Where(bag => bag.BagName.Contains(Name)).ToArray();
            }
            if (!String.IsNullOrEmpty(Cate))
            {
                listBags = listBags.Where(bag => bag.BagCID.Equals(Cate)).ToArray();
            }
            if (String.IsNullOrEmpty(SessionPersister.Username))
            {
                listBags    = listBags.Where(bag => bag.Status.Equals("Active") && bag.Quantity > 0).ToArray();
                redirectURL = "~/Views/Guest.cshtml";
            }
            else
            {
                WCFAccountServiceClient accountServiceClient = new WCFAccountServiceClient();
                if (accountServiceClient.GetUserRole(SessionPersister.Username).Equals("admin"))
                {
                    redirectURL = "~/Views/Admin.cshtml";
                }
                else if (accountServiceClient.GetUserRole(SessionPersister.Username).Equals("customer"))
                {
                    listBags    = listBags.Where(bag => bag.Status.Equals("Active") && bag.Quantity > 0).ToArray();
                    redirectURL = "~/Views/User.cshtml";
                }
            }
            if (page == null || page <= 0)
            {
                page = 1;
            }
            int pageSize = 4;
            int start    = (int)(page - 1) * pageSize;

            ViewBag.pageCurrent = page;
            int   totalPage    = listBags.Count();
            float totalNumsize = (totalPage / (float)pageSize);
            int   numSize      = (int)Math.Ceiling(totalNumsize);

            ViewBag.numSize  = numSize;
            ViewBag.Name     = Name;
            ViewBag.Category = Cate;
            if (listBags.Length == 0)
            {
                listBags     = null;
                ViewBag.Bags = listBags;
            }
            else
            {
                ViewBag.Bags = listBags.Skip(start).Take(pageSize);
            }
            return(View(redirectURL));
        }
Esempio n. 2
0
 public override void OnAuthorization(AuthorizationContext filterContext)
 {
     if (String.IsNullOrEmpty(SessionPersister.Username))
     {
         filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
                                                              (new { controller = "Account", action = "Index" }));
     }
     else
     {
         WCFAccountServiceClient accountServiceClient = new WCFAccountServiceClient();
         CustomPrincipal         customPrincipal      = new CustomPrincipal
                                                            (accountServiceClient.Find(SessionPersister.Username));
         if (!customPrincipal.IsInRole(Roles))
         {
             filterContext.Result = new RedirectToRouteResult(
                 new RouteValueDictionary(new { controller = "Error", action = "Index" }));
         }
     }
 }
        public ActionResult Register(Account account, string Repassword)
        {
            string username = account.UserName;
            WCFAccountServiceClient accountServiceClient = new WCFAccountServiceClient();

            if (ModelState.IsValid)
            {
                bool check = true;

                if (accountServiceClient.Find(username) != null)
                {
                    check = false;
                    ModelState.AddModelError("UserName", "Username is already existed");
                }
                if (accountServiceClient.GetUserPhone(account.PhoneNumber) != null)
                {
                    ModelState.AddModelError("PhoneNumber", "Phone number is already existed");
                    check = false;
                }
                if (!account.Password.Equals(Repassword))
                {
                    ModelState.AddModelError("Repassword", "Does not match with password");
                    check = false;
                }
                if (check)
                {
                    if (accountServiceClient.Register(account))
                    {
                        ViewBag.Message = "Register Successfully!";
                        return(View("~/Views/Login.cshtml"));
                    }
                    else
                    {
                        ViewBag.Message = "Server is currently not available!";
                    }
                }
            }
            return(View("~/Views/Register.cshtml", account));
        }
        public ActionResult Login(string Username, string Password)
        {
            WCFAccountServiceClient accountServiceClient = new WCFAccountServiceClient();
            string role = accountServiceClient.Login(Username, Password);

            if (role != null)
            {
                SessionPersister.Username = Username;
                FormsAuthentication.SetAuthCookie(Username, false);
                if (role.Trim().Equals("admin"))
                {
                    return(RedirectToAction("Admin", "Home"));
                }
                else if (role.Trim().Equals("customer"))
                {
                    return(RedirectToAction("Customer", "Home"));
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid username or password");
            }
            return(View("~/Views/Login.cshtml"));
        }