解决自定义AuthorizeAttribute实现授权管理,AllowAnonymous属性失效导致无法匿名访问控制器的问题

在ASP.NET MVC项目中,一般都要使用身份验证和权限控制,但总有部分网页是可以匿名访问的。使用AllowAnonymous属性就可以指定需要匿名访问的控制器,从而跳过身份验证。

但是今天实现自定义AuthorizeAttribute却遇到了AllowAnonymous属性失效的问题,即使我在控制器、方法上声明AllowAnonymous也依然无法匿名访问,全都需要登陆后才可访问。

namespace System.Web.Mvc
{
    // 摘要: 
    //     表示一个特性,该特性用于标记在授权期间要跳过 System.Web.Mvc.AuthorizeAttribute 的控制器和操作。
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class AllowAnonymousAttribute : Attribute
    {
        // 摘要: 
        //     初始化 System.Web.Mvc.AllowAnonymousAttribute 类的新实例。
        public AllowAnonymousAttribute();
    }
}

按理说声明了AllowAnonymous的控制器或者方法就无需进行身份验证了,这是为什么呢???一定要弄个明白。。。用反编译工具对System.Web.Mvc.dll进行查看,如下图:


注意看红框标注部分代码

bool flag = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
<span style="white-space:pre">	</span>if (flag)
	{
	return;
	}

flag为bool类型变量,调用 filterContext.ActionDescriptor.IsDefined方法,接着看下 IsDefined方法定义

// System.Web.Mvc.ActionDescriptor
/// <summary>Determines whether one or more instances of the specified attribute type are defined for this member.</summary>
/// <returns>true if <paramref name="attributeType" /> is defined for this member; otherwise, false.</returns>
/// <param name="attributeType">The type of the custom attribute.</param>
/// <param name="inherit">true to look up the hierarchy chain for the inherited custom attribute; otherwise, false.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="attritubeType" /> parameter is null.</exception>
public virtual bool IsDefined(Type attributeType, bool inherit)
{
	if (attributeType == null)
	{
		throw new ArgumentNullException("attributeType");
	}
	return false;
}

该方法接收两个参数,第一个attributeType(自定义特性的类型),第二个参数inherit(要查找继承的自定义特性的层次结构链,则为 true;否则为 false),返回结果(如果为此成员定义了 attributeType,则为 true;否则为 false)。

再看看我自定义的 AuthorizeAttribute实现,就不难理解为什么我的AllowAnonymous会失效了。哈哈~~,正如大家所想。只要在自定义AuthorizeAttribute实现里加上红色标注块里的代码,我们就可以再不需要进行授权的控制器或者方法上标注AllowAnonymous了。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值