加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在控制器级别指定操作过滤器与操作方法级别指定,

发布时间:2020-12-16 09:54:26 所属栏目:asp.Net 来源:网络整理
导读:当我创建一个asp.net MVC 5 Web项目时,我检查帐户控制器,我找到以下代码: – [Authorize] public class AccountController : Controller { public AccountController() : this(new UserManagerApplicationUser(new UserStoreApplicationUser(new Applicatio
当我创建一个asp.net MVC 5 Web项目时,我检查帐户控制器,我找到以下代码: –

[Authorize]
    public class AccountController : Controller
    {
        public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

他们在控制器级别指定[授权],在操作方法级别指定[AllowAnonymous].我认为asp.net mvc将首先检查控制器级别的所有动作过滤器,如果它们成功,它将使用action方法调用进行处理.但似乎这不是情况,因为匿名用户可以调用登录操作方法,虽然在控制器级别指定了[Authorize]?那么这里的情景是什么?

谢谢

解决方法

您可以首先查看Authorize属性源代码,了解它的工作原理:
http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/AuthorizeAttribute.cs

仔细查看OnAuthorization方法:您将看到它在操作或控制器上查找AllowAnonymous属性,并在找到任何属性时跳过授权.

bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute),inherit: true)
                                 || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute),inherit: true);

if (skipAuthorization)
{
     return;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读