.NET框架:4.6.1 IIS:10.0
大家好
我建立了一个内部网站,使用 Windows 身份验证。 每个配置看起来都不错:
web.config 指定 Windows 身份验证模式并拒绝匿名用户。 IIS 配置也通过启用 Win Auth 和禁用 Anonymous 进行了很好的配置。 提供者是“NTML”和“Negotiate”。
网站的第一页是一个简单的表,其中包含从第三个服务器:SQL 中选取的数据。
当我使用 ISS Express 进行测试时一切正常,直到我进行“现实世界”测试。 假设我是 userA,我的测试用户是 userB。
userA 拥有高级权限,这意味着他应该从 SQL 获取更多数据。 userB 权限较少,只能看到请求本身过滤的数据。
数据访问不是问题,一切正常并且过滤正确。
问题是,当用户A访问该网站时
我尝试了多次配置更改,但没有成功(每次修改都刷新 IIS 上的 AppPool 和站点)。
使用
System.Web.HttpContext.Current.User.Identity.Name
,或单独使用 User.Identity.Name
。
对每个控制器方法使用 [Authorize]
标头。
在 web.config 和 IIS (system.webServer/caching) 中禁用缓存
在 system.webServer/security/authentication/windowsAuthentication中禁用
authPersisNonNTLM
和
authPersistSingleRequest
基本上,简单地说,问题是 User.Identity.Name 或更全局的 HttpContext.Current.User 对象返回最后一个网站访问者,而不是当前网站访问者。
[编辑]
提供一些详细信息,我的默认页面控制器如下所示:
public ActionResult List()
{
var username = System.Web.HttpContext.Current.User.Identity.Name;
string userDisplayName = ADChecks.GetUserDisplayName(username);
ViewBag.Message = "NewCommers List";
ViewBag.UserDisplayName = userDisplayName;
return View();
}
页面是:
@model IEnumerable<NewComer.Models.NewComerModel>
@{
ViewBag.Title = "New Comer - List";
}
<p>
@if (NewComer.App_Tools.ADChecks.IsRH(System.Web.HttpContext.Current.User.Identity.Name) || NewComer.App_Tools.ADChecks.IsAdmin(HttpContext.Current.User.Identity.Name))
{
<div class="col-4">
<input type="button" value="Create New" class="btn btn-secondary valid w-100 mw-100" onclick="location.href='@Url.Action("Create", "Home")'" />
</div>
}
</p>
<table id="ListViewTable" class="table table-responsive table-striped">
<thead>
<tr>
<th></th>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Contract Type</th>
<th data-bs-toggle="tooltip" data-bs-placement="top" title="MM/DD/YYYY">Date Start</th>
<th data-bs-toggle="tooltip" data-bs-placement="top" title="MM/DD/YYYY">Date End</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
</table>
此页面中有一些 Javascript 在
$.ajax
请求中启动带有数据表的“LoadTable”功能
我最终切换到匿名,并使用登录表单/页面“手动”处理 Windows 身份验证。
由于我不知道如何将 DirectoryServices.UserPrincipal 对象转换为 HttpContext.Current.User 对象,因此我正在使用 Cookie 来检查所有步骤中的身份,并在用户过期时恢复用户登录。