Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在MVC3中,我们如何根据用户的角色将其重定向到默认URL?_Asp.net Mvc 3_Url_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc 3 在MVC3中,我们如何根据用户的角色将其重定向到默认URL?

Asp.net mvc 3 在MVC3中,我们如何根据用户的角色将其重定向到默认URL?,asp.net-mvc-3,url,asp.net-mvc-routing,Asp.net Mvc 3,Url,Asp.net Mvc Routing,如果当前用户是管理员,则在输入根url时应将其重定向到管理员页面???有许多方法(大多数是自定义的),但我会使用默认MVC功能并保持路由不变,但根据安全角色,有两个控制器操作: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Map

如果当前用户是管理员,则在输入根url时应将其重定向到管理员页面???

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

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 } 
               );

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

// actions part of UserController

public ActionResult Index()
{
    ...
}

[Authorize(Roles = "admin")]
[ActionName("Index")]
[AdminsOnly]
public ActionResult IndexAdmin()
{
    ...
}
如果您使用某种自定义机制来定义用户类型/角色成员身份,则始终可以编写自己的授权操作筛选器

但是
authorizationAttribute
不是操作选择器筛选器,因此如果不创建自定义操作选择器筛选器
AdminsOnlyAttribute
,MVC将无法区分两者。这一个将为您执行检查,您不会发现一个请求有多个操作的错误。在编写此自定义筛选器的情况下,您还可以简单地删除
authorized属性
,因为您的操作选择器将检查该属性

其他竞争者 自定义
路线
如果这不是您想要的,您可以随时编写自己的自定义
路由
类,根据用户的用户名/角色成员身份将用户重定向到特定区域。。。虽然重定向也可能是您的
登录操作的一部分

[Authorize(Users = "admin")]
此操作假定应用程序中存在管理区域

自定义路由约束 另一种可能是具有自定义管线约束。因此,您将实际定义两条管线,但其中一条管线具有特定约束:

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

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

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

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 } 
);

输入哪个路由url?您是如何在同一控制器上用相同的名称和参数定义两个操作的?这不是有效的C代码。若用户已经登录,并在浏览器中键入根URL,那个么???在MVC中,它将从Global.asax获取根URL。假设“ABC”角色的根URL为/User,但“XYZ”角色的根URL不可访问,然后用户的“XYZ”角色类型为根URL,则他将获得安全消息,但是我需要将他重定向到他的默认url。所以问题是我可以从全局重定向他吗?asax?@Arun:我的第一个案例中有两种操作方法,在这个场景中效果很好,尽管在所有需要基于用户角色的不同场景中都会有耦合操作。但除此之外,正如我提到的,还有其他的定制方式。您可以编写一个考虑您的角色的路由约束。或者有一个自定义的
Route
类可以知道这一点。我可能会首先尝试约束。thanx Robert非常感激:)是的,这可能是一个选项,但这将导致双重响应时间…客户不会高兴
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();
    }
}