【问题标题】:restrict user access to controller based on property in object (asp.net mvc)根据对象中的属性限制用户对控制器的访问(asp.net mvc)
【发布时间】:2011-03-21 06:49:42
【问题描述】:

控制用户对控制器的访问的最佳方式是什么。我有一个带有属性的本地用户对象(布尔值 - “IsSubscribed”)。只有当该值为 true 时,用户才能访问控制器。

注意事项:

我使用表单身份验证,但没有 .net 会员/个人资料等。

mvc 版本 2

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    你可以编写一个自定义的Authroize 属性:

    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var isAuthorized = base.AuthorizeCore(httpContext);
            if (isAuthorized)
            {
                // Perform your custom authorization and return true/false
            }
            return isAuthorized;
        }
    }
    

    然后用这个属性装饰你的控制器/动作。

    【讨论】:

    • 1. 那么我们是否像 [Authorize(...)] public ActionResult myAction() (或)[CustomAuthorize(...)] public ActionResult myAction()
    • @maxxxee, [CustomAuthorize] public ActionResult MyAction() { ... }
    猜你喜欢
    • 1970-01-01
    • 2010-11-13
    • 2013-12-08
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多