C#通过控制器进行授权

本文关键字:授权 控制器 | 更新日期: 2024-09-20 16:03:36

我有我制作的购物车控制器,因此用户无法继续,除非购物车中有项目,否则不会显示链接。

然而,他们仍然可以只键入URL并转到AddressAndPayment页面,我如何在AddressAndPayments控制器中使用类似的If语句,就像在Cart控制器中使用的那样,来阻止用户查看页面。

推车控制器

 public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
                // Set up the ViewModel
                ShoppingCartViewModel viewModel = new ShoppingCartViewModel
                {
                    CartItems = cart.GetCartItems(),
                    CartTotal = cart.GetTotal()
                };

            if (viewModel.CartItems.Any())
            {
                ViewBag.CartStatus = "Proceed to checkout or ";
                ViewBag.Link = "AddressAndPayment";
                ViewBag.Link2 = "Checkout";
            }
            else
            {
                ViewBag.CartStatus = "Cart is empty please ";
                ViewBag.Link = "Index";
                ViewBag.Link2 = "Store";
            }

            // Return the view
            return View(viewModel);
        }

地址和校验控制器

public ActionResult AddressAndPayment()
    { 
            return View();
    }

    /// <summary>
    /// Gets the address and payment from user
    /// </summary>
    /// <param name="values">payment values</param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult AddressAndPayment(FormCollection values)
    {
        var order = new Order();
        TryUpdateModel(order);
                order.Username = User.Identity.Name;
                order.OrderDate = DateTime.Now;
                //Order gets saved
                storeDB.Orders.Add(order);
                storeDB.SaveChanges();
                //Order gets processed
                var cart = ShoppingCart.GetCart(this.HttpContext);
                cart.CreateOrder(order);
                //NEW TEST IF SAVES
                storeDB.SaveChanges();
                //Model.Product.stock = item.Product.stock - item.count;

                return RedirectToAction("Complete",
                    new { id = order.OrderId });
    }

C#通过控制器进行授权

如果购物车是空的,为什么不直接重定向到购物车页面?

public ActionResult AddressAndPayment()
{ 
    var cart = ShoppingCart.GetCart(this.HttpContext);
    if(!cart.GetCartItems().Any())
        return RedirectToAction("Index", "Cart");  // assuming Cart as controller name and Index as action name
    return View();
}

您可以对POST操作执行相同的操作。

交叉关注点应该作为过滤器来实现,这样您就可以在操作/控制器之间重用它们。在这种情况下,您可以制作一个MustHaveCartItemsAttribute

using System;
using System.Web.Mvc;
using System.Web.Routing;
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class MustHaveCartItemsAttribute : Attribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var cart = ShoppingCart.GetCart(filterContext.HttpContext);
        var ViewBag = filterContext.Controller.ViewBag;
        if (!cart.GetCartItems.Any())
        {
            ViewBag.CartStatus = "Cart is empty please ";
            ViewBag.Link = "Index";
            ViewBag.Link2 = "Store";
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
            new { controller = "Cart", action = "Index" }));
        }
    }
}

用法

[MustHaveCartItems]
public ActionResult AddressAndPayment()
{ 
        return View();
}