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

前端 未结 4 1531
时光取名叫无心
时光取名叫无心 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:31

    If the id is passed as request parameter (GET or POST) or as a route data parameter:

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        // first look at routedata then at request parameter:
        var id = (httpContext.Request.RequestContext.RouteData.Values["id"] as string) 
                 ??
                 (httpContext.Request["id"] as string);
        if (id == "8")
        {
            return base.AuthorizeCore(httpContext);
        }
        return true;
    }
    

提交回复
热议问题