将 RedirectToRouteResult 与 Area 一起使用失败
Use RedirectToRouteResult with Area failed
我正在使用自定义过滤器来检查用户对特定操作的访问权限,并且我有一个名为 Admin 的区域。 filter redirect unauthorized user 时,重定向到区域内,而不是路由目录下的视图。
例如,当我现在访问 http://localhost/admin/roles I expect that unauthorized user will be redicreted to http://localhost/authorized but not to http://localhost/admin/authorized 时。
以下是我如何使用过滤器:
public override void OnAuthorization(AuthorizationContext filterContext)
{
DigitalHubOnlineStoreEntities db = new DigitalHubOnlineStoreEntities();
RbacUser requestingUser = new RbacUser();
var controllerid = RbacUser.GetControllerId(filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
var actionid = RbacUser.GetActionId(filterContext.ActionDescriptor.ActionName, controllerid);
if (!requestingUser.GetUserPermission(HttpContext.Current.User.Identity.Name, actionid, controllerid))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Unauthorized" } });
}
}
您需要用空字符串指定 Area
:
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "action", "Index" },
{ "controller", "Unauthorized" },
{ "Area", String.Empty }
});
我正在使用自定义过滤器来检查用户对特定操作的访问权限,并且我有一个名为 Admin 的区域。 filter redirect unauthorized user 时,重定向到区域内,而不是路由目录下的视图。
例如,当我现在访问 http://localhost/admin/roles I expect that unauthorized user will be redicreted to http://localhost/authorized but not to http://localhost/admin/authorized 时。
以下是我如何使用过滤器:
public override void OnAuthorization(AuthorizationContext filterContext)
{
DigitalHubOnlineStoreEntities db = new DigitalHubOnlineStoreEntities();
RbacUser requestingUser = new RbacUser();
var controllerid = RbacUser.GetControllerId(filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
var actionid = RbacUser.GetActionId(filterContext.ActionDescriptor.ActionName, controllerid);
if (!requestingUser.GetUserPermission(HttpContext.Current.User.Identity.Name, actionid, controllerid))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Unauthorized" } });
}
}
您需要用空字符串指定 Area
:
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "action", "Index" },
{ "controller", "Unauthorized" },
{ "Area", String.Empty }
});