[英]Use NTFS security in IIS for given user
我有一个网站试图从限制特定AD组/用户的文件夹中检索文档。 鉴于使用Windows身份验证进行身份验证的用户,我希望尝试检查他们是否可以访问特定位置并根据需要显示/隐藏文档。 我的印象是使用Windows身份验证和ASP.NET模拟会在执行请求时使用当前用户的凭据,而不是应用程序池的凭据,但似乎并非如此,因为我是管理员能够在Windows资源管理器中打开受限文件夹,但我无法从我的应用程序中读取它们。
有没有办法使用当前请求用户的凭据来访问本地文件系统上的文件?
在我的web.config中:
<appSettings>
<add key="documentFolder" value="C:\Users\auser\source\repos\myapp\myapp\docs\folder1,C:\Users\auser\source\repos\myapp\myapp\docs\folder2,C:\Users\auser\source\repos\myapp\myapp\docs\secured" />
<add key="securityFolder" value="C:\Users\auser\source\repos\myapp\myapp\docs\secured"/>
</appSettings>
<system.web>
<authentication mode="Windows" />
<identity impersonate="true"/>
<authorization>
<deny users="?" />
</authorization>
</system.web>
我试图在两个地方访问文件。 首先,我索引它们(现在这从Application_Start运行):
var dirs = ConfigurationManager.AppSettings["documentFolder"].ToString().Split(',');
foreach (var dir in dirs)
{
foreach (var file in Directory.GetFiles(dir, "*.pdf", SearchOption.TopDirectoryOnly))
{
try
{
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); // <-- Fails with "Access to the path 'C:\Users\auser\source\repos\myapp\myapp\docs\secured' is denied."
}
catch (Exception ex)
{
logger.Error("Failed to read file.", ex);
}
}
}
稍后我会尝试检查用户是否有权访问:
protected void Button1_Click(object sender, EventArgs e)
{
bool canSeeSecurityDocuments;
try
{
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(ConfigurationManager.AppSettings["securityFolder"].ToString()); <-- Fails with "Attempted to perform an unauthorized operation."
canSeeSecurityDocuments = true;
}
catch (Exception ex)
{
canSeeSecurityDocuments = false;
}
}
从procmon:
期望的访问:读取属性,读取控制
性格:开放
选项:打开重分析点
属性:不适用
ShareMode:读,写,删除
AllocationSize:不适用
模仿:mydomain \\ myuserid < - 编辑,但正确。
基于对集成管道和经典管道的研究,我将GetFiles
调用移动到一个不同的位置,并将其包装在WindowsImpersonationContext
以查看是否强制模拟,但它没有。 (从这个答案 ):
var current = System.Security.Principal.WindowsIdentity.GetCurrent();
logger.Debug(current.Name); <-- This is my identity
WindowsIdentity clientId = (WindowsIdentity)User.Identity;
using (WindowsImpersonationContext wic = clientId.Impersonate())
{
current = System.Security.Principal.WindowsIdentity.GetCurrent();
logger.Debug(current.Name); <-- This is still my identity
// call to GetFiles, as above. Still fails.
}
current = System.Security.Principal.WindowsIdentity.GetCurrent();
logger.Debug(current.Name); <-- still me. Nothing seems to change.
问题是我正在使用线程池来执行一些后台工作(索引文件)。 根据这个SO答案 ,似乎线程池不会遗留用于创建它的凭据。 我应该在实际的线程中检查以查看上下文是什么...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.