【问题标题】:Authorize Attribute extentions new keyword授权属性扩展新关键字
【发布时间】:2013-02-19 05:44:37
【问题描述】:

我想在不创建自定义授权对象的情况下扩展授权属性的关键字过滤器。

我知道内置关键字是角色、用户。我想添加一个新的关键字调用级别,看看用户访问级别是否大于某个级别。

这将允许我只为用户创建一个角色。

这个的用法是

[Authorize(level > 1)]

如果您需要更多信息以便更好地了解我正在尝试解决的问题,请告诉我。

我已查看但未能解决此问题。你能提供一个代码示例吗?这就是我的目的。使用级别过滤器时出现错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
[Auth(Level > 1)]
public ActionResult Index()
{
}

public class Auth : AuthorizeAttribute
{
    public int Level { get; set; }
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      base.OnAuthorization(filterContext);
    }
}

【问题讨论】:

    标签: c# asp.net-mvc authorize-attribute


    【解决方案1】:

    这不可能只是将新过滤器添加到内置AuthorizeAttribute。您需要创建一个继承自 AuthorizeAttribute 的新属性。

    有大量关于创建自定义 AuthorizeAttributes 的博客文章和问题。

    编辑: 好的,好的开始。你的问题是你不能像你正在尝试的那样做Level > 1。假设您尝试说当用户 Level 大于 1 时要通过什么身份验证,您可以更改属性中的属性以表明这一点。

    例如

    public class Auth : AuthorizeAttribute
    {
        public int MinLevel { get; set; }
    
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            //This is where you will need to determine if the user is authorized based upon the minimum level.
            base.OnAuthorization(filterContext);
        }
    }
    

    【讨论】:

    • 我已经看过并且无法弄清楚如何让我的自定义身份验证中的新关键字起作用。你能提供样品吗?这就是我到目前为止所得到的。
    • @user2085818 我已经为你举了一个例子
    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    相关资源
    最近更新 更多