我有一个我无法解决的场景:
我正在为 mvc 创建自己的自定义授权属性。我想添加的主要功能是能够在用户不在特定角色时更改用户被重定向的位置。如果他们没有经过身份验证,我不介意系统将它们发送回登录页面,但是如果他们经过身份验证但不允许访问该操作方法,我想选择将它们发送到哪里。
这是我想做的事情:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public string Action;
public string Controller;
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
// if User is authenticated but not in the correct role
string url = Url.Action(this.Action, this.Controller);
httpContext.Response.Redirect(url);
}
}
作为额外的奖励,我希望在进行重定向之前能够访问 ViewContext 和 TempData。
关于如何在属性中实例化 UrlHelper 和 ViewContext 有什么想法吗?