我已经读过几个关于这个话题的问题,
例如 here,here,here和 here;
但没有一个在我的情况下提供了一个工作的解决方案。
例如 here,here,here和 here;
但没有一个在我的情况下提供了一个工作的解决方案。
我想做什么:
为仅由我们自己的员工使用的Web应用程序实施Windows身份验证。这样他们不应该需要登录到应用程序,但已经通过登录窗口的方式进行身份验证。
此外,我需要限制应用程序的某些区域,根据用户可能被分配到的Active Directory安全组。
所以我想要能够装饰控制器/操作
- [Authorize(Roles="SomeRole")]
我试过:
我有
- <authentication mode="Windows" />
在我的web.config。我已经添加了一个< roleManager>在一些链接到上面的帖子中找到。目前我有这个角色经理
- <roleManager defaultProvider="WindowsProvider"
- enabled="true"
- cacheRolesInCookie="false">
- <providers>
- <add
- name="WindowsProvider"
- type="System.Web.Security.WindowsTokenRoleProvider" />
- </providers>
- </roleManager>
如this帖子中所示。
因为它是,如果我用[授权]装饰控制器,我可以访问它。
然而:
我可以看到在我的网络上的用户设置,我是AD安全组名为“IT”的一部分。但是如果我用[Authorize(Roles =“IT”)]装饰相同的控制器,我得到由asp.net开发服务器为401未授权的空白屏幕。这是意想不到的。我认为我应该能够查看该页面,因为我登录到Windows和组“IT”的一部分。
我在这个主题上找到的大多数东西使得它听起来很简单,完成我想做的,但我显然缺少这里的东西。
解决方法
对于dev我使用IISExpress
与开发服务器属性的MVC项目建立起来
禁用匿名身份验证,并启用Windows身份验证。
使用我们的TFS构建服务器来部署Web配置,以测试和发布服务器,其身份验证也如上所述设置,并在这些位置工作。
与开发服务器属性的MVC项目建立起来
禁用匿名身份验证,并启用Windows身份验证。
使用我们的TFS构建服务器来部署Web配置,以测试和发布服务器,其身份验证也如上所述设置,并在这些位置工作。
在我的web.config我有。
- <system.web>
- ....
- <authentication mode="Windows" />
- <authorization>
- <deny users="?" />
- </authorization>
- <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
- <providers>
- <clear />
- <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
- </providers>
- </roleManager>
- ....
- </system.web>
我可以用
- [Authorize(Roles = @"DOMAIN\ADGroup")]
- Public ActionResult Index()
- {...}
要么
- public ActionResult Index()
- {
- var User = System.Web.HttpContext.Current.User;
- if (User.IsInRole("DOMAIN\\ADGroup"))
- {
- return RedirectToAction("IRSAdmin");
- }
- return View();
- }