【问题标题】:In MVC 3 how we can redirect user to default URL, depending on its role?在 MVC 3 中,我们如何根据角色将用户重定向到默认 URL?
【发布时间】:2013-01-24 09:30:26
【问题描述】:
public static void RegisterRoutes(RouteCollection routes)
           {
               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
               routes.MapRoute(
                   "Default", // Route name
                   "{controller}/{action}/{id}", 
                   new { controller = "User", action = "Index", id = UrlParameter.Optional } 
               );

           }

如果当前用户是管理员,那么他应该在输入 root url 时重定向到管理员页面???

【问题讨论】:

  • 输入哪个路由url?

标签: asp.net-mvc-3 url asp.net-mvc-routing


【解决方案1】:

有很多方法(大多数是自定义的),但我会使用默认的 MVC 功能并保持路由不变,而是根据安全角色有两个控制器操作:

// actions part of UserController

public ActionResult Index()
{
    ...
}

[Authorize(Roles = "admin")]
[ActionName("Index")]
[AdminsOnly]
public ActionResult IndexAdmin()
{
    ...
}

当用户成为特定角色的成员时,这将自动运行第二个。但是,如果您只有一个特定用户(管理员),那么您可以将该属性更改为:

[Authorize(Users = "admin")]

如果您使用某种自定义机制来定义用户类型/角色成员资格,您始终可以编写自己的授权操作过滤器。

AuthoriseAttribute 不是动作选择器过滤器,因此 MVC 无法在不创建自定义动作选择器过滤器 AdminsOnlyAttribute 的情况下区分两者。这会为您进行检查,并且您不会错误地认为请求有多个操作。在编写此自定义过滤器的情况下,您还可以简单地删除 AuthorizeAttribute,因为您的操作选择器已经检查过了。

其他竞争者

自定义Route

如果这不是您想要的,您可以随时编写自己的自定义 Route 类,根据用户的用户名/角色成员身份将用户重定向到特定区域...虽然重定向也可以是您的 @987654328 的一部分@动作

[HttpPost]
public ActionResult Login(LoginCredentials user)
{
    // authenticate
    ...
    if (User.IsInRole("admin"))
    {
        return this.RedirectToAction("Index", "User", new { area = "Admin" });
    }
    return this.RedirectToAction("Index", "User");
}

此操作假定您的应用程序中有管理区域。

自定义路由约束

另一种可能性是有自定义路由约束。所以你实际上会定义两条路线,但一条有特定的约束:

routes.MapRoute(
    "Admin", // Route name
    "{controller}/{action}/{id}", 
    new { area = "Admin", controller = "User", action = "Index", id = UrlParameter.Optional },
    new { isAdmin = new AdminRouteConstraint() }
);
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", 
    new { controller = "User", action = "Index", id = UrlParameter.Optional } 
);

通过这种方式,您可以将管理员路由到应用程序的 admin 区域,并为他们提供他们在那里拥有的特定功能。但这并不意味着他们需要一个管理区域。这只是我的路线定义。您可以按照自己的方式定义路由默认值。

【讨论】:

  • 您是如何设法在同一个控制器上定义两个具有相同名称和相同参数的操作?这不是有效的 C# 代码。
  • 如果用户已经登录并且他在浏览器中输入了根 URL 然后 ???与在 MVC 中一样,它将从 Global.asax 获取根 URL。假设根 URL 是“ABC”角色的 /User,但对于“XYZ”角色,它不可访问,然后具有“XYZ”角色类型根 URL 的用户会收到安全消息,但我需要将他重定向到他的默认 url。
  • 所以问题是我可以从 Global.asax 重定向他吗?
  • @Arun:我的第一个具有两种操作方法的案例在这种情况下工作得很好,尽管您将在所有需要基于用户角色的不同场景的情况下进行耦合操作。但除此之外,正如我提到的,还有其他 custom 方式。您可以编写一个将您的角色考虑在内的路线约束。或者有一个自定义的Route 类会意识到这一点。我可能会先尝试约束。
  • thanx Robert 非常感谢 :)
【解决方案2】:

您可以在您的索引操作中实现此重定向:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        if (User.IsInRole("admin"))
        {
            // The user is an administrator => redirect him to his own page
            return RedirectToAction("SomeActionName", "SomeControllerName", new { area = "admin" });
        }

        // the use is either not authenticated or not in the admin role => render some view
        return View();
    }
}

【讨论】:

  • 是的,这可能是一种选择,但这会导致双倍的响应时间......客户不会高兴
猜你喜欢
  • 2017-11-29
  • 1970-01-01
  • 2011-06-05
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
相关资源
最近更新 更多