我能找到一些基于角色的身份验证的例子,但这不是一个身份验证问题。我有三种用户类型,其中一种我想要有一个不同的默认起始页面。路由配置在用户信息可用之前初始化。
简而言之:如果角色A或角色B从下面开始。
对于那些需要的人 - 这是属性。
简而言之:如果角色A或角色B从下面开始。
Controller: Home
Action: Index
否则:
Controller: Other
Action: OtherIndex
这应该在哪里以及如何实现?
编辑
这只应该在网站首次访问时发生,其他用户可以转到Home / Index,但不是默认情况下。
编辑
使用Brad的建议,我创建了一个重定向属性,具有他的重定向逻辑,并将其应用于Index。然后我为该页面创建了另一个操作。这样,如果我确实需要允许访问HomeIndex,我可以专门将其分配给Home / HomeIndex,并且使用默认路由的任何内容都可以访问Home / Index。
[RedirectUserFilter]
public ActionResult Index()
{
return HomeIndex();
}
对于那些需要的人 - 这是属性。
public class RedirectUserFilterAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
if (!HttpContext.Current.User.IsInRole("Role A")
&& !HttpContext.Current.User.IsInRole("Role B"))
{
actionContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {
{ "Controller", "OtherController" },
{ "Action", "OtherAction" } });
}
}
}
ActionFilter
或AuthorizationFilter
是更好的选择,这样你就可以将规则应用于所有请求,而不必将代码添加到每个操作方法中。 - asymptoticFaultActionFilter
或AuthorizationFilter
在路由应用后检查标志,并在必要时重定向。 - asymptoticFault