我有一个控制器,只有在加载特定参数时才需要请求授权。比如当参数ID为8时。
我想出了使用自定义验证属性的方法,代码如下:
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (/* Action's inputparameter ID = 8 */)
{
return base.AuthorizeCore(httpContext);
}
return true;
}
}
我的动作看起来像这样(并不是很有趣)
[MyAuthorize]
public ActionResult Protected(int id)
{
/* custom logic for setting the viewmodel from the id parameter */
return View(viewmodel);
}
问题就像你所看到的一样,我不知道如何在授权属性中检查那个ID参数。你能帮我想一个解决方案吗?