路由不同组的ASP.NET Windows身份验证

时间:2019-03-03 05:12:27

标签: asp.net asp.net-mvc windows-authentication

我正在尝试建立一个应用程序,该应用程序将使用Windows身份验证根据Active Directory中它们所在的组路由到我的应用程序的不同区域。

例如,我有一个管理员组中的用户被路由到一个区域,而一个用户组被路由到另一个区域。
所有用户都将使用相同的URL进入该应用程序,但是根据他们所在的组,会将其转发到其适当的区域。

我不确定该在哪里处理?如果有人链接到已经回答了该问题的情况,那将是很好的。

我们非常感谢您的帮助和指导,如果您需要更多信息或本说明中不清楚,请告诉我。

2 个答案:

答案 0 :(得分:3)

ASP.NET MVC不支持基于角色/组的路由,您可以像下面这样重定向到Home Controller Index中的适当操作。

if (User.IsInRole("Admin"))
{
    return RedirectToAction("Index", "AdminController");
}

答案 1 :(得分:1)

您正在搜索此内容,可能是:

1。

public ActionResult LogIn(string userName, string password)
{

  if (!Membership.ValidateUser(userName, password))
  {
    Redirect("http://goaway.com");
  }

  string[] userRoles = Roles.GetRolesForUser();
  string
    controller,
    action;

  if (userRoles.Contains("Role1"))
  {
    controller = "Role1";
    action = "Index";
  }
  else if (userRoles.Contains("Role2"))
  {
    controller = "Role2";
    action = "Index2";
  }
  else
  {
    throw new InvalidOperationException("Bad user!");
  }

  return RedirectToActionPermanent(action, controller);
}
<configuration>
  <system.web>
    <roleManager enabled="true" />
  </system.web>
</configuration>

2。

public ActionResult LogIn2(string userName, string password)
{

  if (!Membership.ValidateUser(userName, password))
  {
    Redirect("http://goaway.com");
  }

  string
    controller,
    action;

  if (User.IsInRole("Admins"))
  {
    controller = "Admins";
    action = "LogIn";
  }
  else if (User.IsInRole("Editors"))
  {
    controller = "Editors";
    action = "LogIn";
  }
  else
  {
    controller = "LogOut";
    action = "LogoOut";
  }

  return RedirectToActionPermanent(action, controller);
}