使用活动目录角色提供程序MVC4进行授权

时间:2013-12-20 16:19:18

标签: asp.net-mvc-4 web-config authorization roleprovider

我正在构建一个MVC4应用程序,供企业环境内部使用。我使用Windows身份验证,工作正常,但我在使用Active Directory组作为授权角色时遇到了麻烦。

我的Web.config看起来像这样:

<authentication mode="Windows" />        
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
  <providers>
    <clear />
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
  </providers>
</roleManager>        
<authorization>
  <deny users="?" />    
</authorization>

当我使用用户授权时,它可以正常工作:

[Authorize(Users = @"DOMAIN\User1, DOMAIN\User2")]
public ActionResult Create()
{
    return View();
}

但是当我使用角色时,它只是不允许该组中的用户访问此操作:

[Authorize(Roles = @"Domain\Group")]
public ActionResult Create()
{
    return View();
}

我也试过指定没有域的组,因为我在其他回复中读到了,但没有运气......我想我在Web.config中遗漏了一些东西,但我不确定是什么......

我避免使用自定义角色提供程序,因为MVC4应该在没有自定义角色提供程序的情况下实现这一目标(或者至少是我认为的那样)

任何人都可以帮我吗?

提前致谢!

1 个答案:

答案 0 :(得分:19)

我发现了哪个问题。在阅读了有关machine.config here的一些信息之后,我检查了我已经应用了正确的配置。

Fianlly我让它像这样工作:

[Authorize(Roles = "Domain\\Group")]
public ActionResult Create()
{
    return View();
}

问题在于我输入群组的方式。

我希望这可以帮助其他人。