【问题标题】:create the authorize filter with parameter asp.net mvc使用参数 asp.net mvc 创建授权过滤器
【发布时间】:2011-03-28 18:32:37
【问题描述】:

我必须在 asp.net mvc 中开发一个授权过滤器。我的站点中有五类用户,并且我的站点使用自定义创建的身份验证系统。现在我有一个控制器操作,应该可以访问其中的 3 个五种类型的用户。如何创建一个过滤器(基本上是授权)并使用它来满足我的要求?我想我需要创建带有参数的授权过滤器。我应该能够使用这样的东西。

授权[UsersType="admin,accountant,operator"]

使用的技术:Asp.net MVC

提前致谢

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:
    1. 创建一个继承MVC的AuthorizeAttribute类的Attribute类。
    2. 在您的属性类中创建一个接受参数UsersType 的构造函数
    3. 覆盖所需的 AuthorizeAttribute 的适当方法。
    4. 在适当的覆盖方法中解析参数。

    public class AuthorizeUserAttribute :AuthorizeAttribute
    {
        private string[] _userType { get; set; }
    
        public AuthorizeUserAttribute(string UsersType)
        {
            // parse your usertypes here.
        }
    
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            // do the appropriate assigning and authorizing of methods here
            ....
            base.OnAuthorization(filterContext);
        }
    }
    

    现在您可以在控制器的方法中添加一个属性

    [AuthorizeUser("admin,accountant,operator")]
    public ActionMethod Index()
    {
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2014-07-18
      • 2019-09-12
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2013-04-09
      相关资源
      最近更新 更多