C# MVC 3自定义授权属性定义始终返回true

C# MVC 3自定义授权属性定义始终返回true,c#,asp.net-mvc-3,attributes,C#,Asp.net Mvc 3,Attributes,我定义了一个自定义授权属性,它自动应用于解决方案中的所有操作。 在OnAuthorize方法中,我使用IsDefined方法查找是否定义了另一个属性,但它似乎总是返回false 编辑:AuthorizeAttr属性在Global.asax中的RegisterGlobalFilters函数中设置,Anon属性直接标记在不需要授权的操作上方 这是我的密码: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowM


我定义了一个自定义授权属性,它自动应用于解决方案中的所有操作。 在OnAuthorize方法中,我使用IsDefined方法查找是否定义了另一个属性,但它似乎总是返回false

编辑:AuthorizeAttr属性在Global.asax中的RegisterGlobalFilters函数中设置,Anon属性直接标记在不需要授权的操作上方

这是我的密码:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class Anon : Attribute { }

public class Role : Attribute
{
    public int Id;
    public Role(int id)
    {
        Id = id;
    }
}

public class AuthorizeAttr : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (!(filterContext.ActionDescriptor.IsDefined(typeof(Anon), false)) || !(filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false)))
        {
            Procurement.User u = MvcApplication.GetCurrentUser(filterContext.HttpContext);
            if (u == null || !u.enabled)
                filterContext.Result = new RedirectResult("/Session/Login?msg=You must log in to use this site.&ReturnUrl=" + filterContext.RequestContext.HttpContext.Request.RawUrl);
            if (filterContext.ActionDescriptor.IsDefined(typeof(Role), false))
            {
                object[] criterias = filterContext.ActionDescriptor.GetCustomAttributes(typeof(Role), false);
                bool authorized = true;
                for (int x = 0; x < criterias.Length; x++)
                {
                    if (((Role)criterias[x]).Id > u.roleId)
                    {
                        authorized = false;
                        break;
                    }
                }

                if (!authorized)
                {
                    ContentResult C = new ContentResult();
                    C.Content = "<h1><b>The resource is unavailable!</b></h1>";
                    filterContext.Result = C;
                }
            }
        }

    }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple=false,Inherited=true)]
公共类Anon:属性{}
公共类角色:属性
{
公共int Id;
公共角色(int-id)
{
Id=Id;
}
}
公共类AuthorizeAttr:AuthorizeAttribute
{
授权时的公共覆盖无效(AuthorizationContext filterContext)
{
如果(!(filterContext.ActionDescriptor.IsDefined(typeof(Anon),false))| |!(filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon,false)))
{
Procurement.User u=mvcapapplication.GetCurrentUser(filterContext.HttpContext);
如果(u==null | |!u.enabled)
filterContext.Result=new RedirectResult(“/Session/Login?msg=您必须登录才能使用此网站。&ReturnUrl=“+filterContext.RequestContext.HttpContext.Request.RawUrl”);
if(filterContext.ActionDescriptor.IsDefined(typeof(Role),false))
{
object[]criterias=filterContext.ActionDescriptor.GetCustomAttributes(typeof(Role),false);
bool=true;
对于(int x=0;xu.roleId)
{
授权=假;
打破
}
}
如果(!授权)
{
ContentResult C=新的ContentResult();
C.Content=“资源不可用!”;
filterContext.Result=C;
}
}
}
}
}

在布尔代数中

isDefinedOnAction || isDefinedOnController
是:

因此,您可能需要一个
&&
条件:

public override void OnAuthorization(AuthorizationContext filterContext)
{
    var isDefinedOnAction = filterContext.ActionDescriptor.IsDefined(typeof(Anon), false);
    var isDefinedOnController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false);
    if (!isDefinedOnAction && !isDefinedOnController)
    {
        ... the Anon attribute is not present neither on an action nor on a controller
        => perform your authorization here
    }
}
或者如果您想要
|

public override void OnAuthorization(AuthorizationContext filterContext)
{
    var isDefinedOnAction = filterContext.ActionDescriptor.IsDefined(typeof(Anon), false);
    var isDefinedOnController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false);
    if (isDefinedOnAction || isDefinedOnController)
    { 
        ... the attribute is present on either a controller or an action
        => do nothing here
    }
    else
    {
        ... perform your authorization here
    }
}

显然,第一个更具可读性。

啊!当然!因为我否定了结果,所以它正在检查它是否不存在,并且只有其中一个没有该属性,才允许执行if语句中的代码。很抱歉发布了一个简单的逻辑问题,请单击此处寻求帮助@Mats Edvinsson,我已经测试了代码,它运行得很好。在if条件之前,
isDefinedOnAction
isDefinedOnController
变量的值是多少?它们都是假的吗?您是否使用
[Anon]
属性装饰控制器或操作?两者都是false,是的,我已在HomeController索引和此代码重定向到的登录页面上定义了[Anon]属性。它只是在重定向循环中结束。@Mats Edvinsson,我刚刚测试了您描述的场景,它对我来说非常好。您确定请求的是
索引
操作吗?当您第一次进入属性时,
filterContext.ActionDescriptor.ActionName
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName
的值是什么?首先,它访问重定向到需要授权的操作的索引,该操作重定向到登录页面(具有[Anon]属性)但是登录页面一直重定向到自身。刚才就是这样,现在它试图访问索引,只是因为某种原因挂起。
public override void OnAuthorization(AuthorizationContext filterContext)
{
    var isDefinedOnAction = filterContext.ActionDescriptor.IsDefined(typeof(Anon), false);
    var isDefinedOnController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false);
    if (isDefinedOnAction || isDefinedOnController)
    { 
        ... the attribute is present on either a controller or an action
        => do nothing here
    }
    else
    {
        ... perform your authorization here
    }
}