Using action parameters in custom Authorization Attribute in ASP.NET MVC3

前端 未结 4 1537
时光取名叫无心
时光取名叫无心 2020-12-13 19:42

I have a controller which should only request authorization when loaded with specific parameters. Like when the parameter ID is 8 for example.

I came up with using a

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 20:21

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var rd = httpContext.Request.RequestContext.RouteData;
            string currentAction = rd.GetRequiredString("action");
            string actionparam =Convert.ToString(rd.Values["param"]);
    
            if (id == actionparam)
            {
                return base.AuthorizeCore(httpContext);
            }
    return true;
     }
    

提交回复
热议问题