gpt4 book ai didi

asp.net-mvc - 使用事件目录组作为角色的 Windows 身份验证

转载 作者:行者123 更新时间:2023-12-03 08:41:00 25 4
gpt4 key购买 nike

我已经阅读了关于这个主题的几个问题,
here , here , herehere ;
但在我的情况下,没有一个提供有效的解决方案。

我想做什么:

为仅由我们自己的员工使用的 Web 应用程序实现 Windows 身份验证。这样他们就不需要登录应用程序,但已经通过登录 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 中发现的邮政。

事实上,如果我用 [Authorize] 装饰一个 Controller ,我可以正常访问它。

但是:

我可以在网络上的用户设置中看到,我是名为“IT”的 AD 安全组的一部分。但是如果我用 [Authorize(Roles="IT")] 装饰同一个 Controller 我得到了 401 未授权的 asp.net 开发服务器提供的空白屏幕。 这是出乎意料的。 我认为我应该能够在我登录到 Windows 并且是“IT”组的一部分时查看该页面。

我在这个主题上找到的大部分内容都使完成我想要做的事情听起来很简单,但我显然在这里遗漏了一些东西。

最佳答案

对于开发人员,我正在使用 IISExpress
设置 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();
}

在我记得注销并重新登录后,我获得了 AD 组的权限。

关于asp.net-mvc - 使用事件目录组作为角色的 Windows 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com