Set*_*eth 3 asp.net-mvc asp.net-mvc-4
我已经能够找到几个基于角色的身份验证的示例,但这不是身份验证问题.我有三种用户类型,其中一种我希望有一个不同的默认起始页面.在用户信息可用之前初始化Route-config.
在坚果壳中:如果角色A或角色B从下面开始
Controller: Home
Action: Index
Run Code Online (Sandbox Code Playgroud)
其他:
Controller: Other
Action: OtherIndex
Run Code Online (Sandbox Code Playgroud)
应该在何处以及如何实施?
编辑
这应该仅在第一次访问站点时发生,其他用户可以转到Home/Index,而不是默认情况下.
编辑
使用Brad的建议我使用他的重定向逻辑创建了一个重定向属性,并将其应用于Index.然后我为页面创建了另一个操作.这样,如果我确实需要允许访问HomeIndex,我可以专门为它分配Home/HomeIndex,任何使用默认路由的东西都可以访问Home/Index
[RedirectUserFilter]
public ActionResult Index()
{
return HomeIndex();
}
Run Code Online (Sandbox Code Playgroud)
对于那些需要它的人 - 这是属性
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" } });
}
}
}
Run Code Online (Sandbox Code Playgroud)
public class HomeController : Controller
{
public ActionResult Index()
{
if (
!User.IsInRole("Something") &&
!User.IsInRole("Role B")
) return RedirectToAction("OtherIndex");
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
同样如此OtherController.
或者,您可以创建自己的ActionFilter查找常规命名的操作(如IndexRolenameover Index)
| 归档时间: |
|
| 查看次数: |
1781 次 |
| 最近记录: |