C# Asp.net web服务需要返回Windows主体的NTFS文件权限,而不是服务本身
我们正在运行一个ASP.net web服务,该服务需要能够根据用户或其所属广告组的NTFS权限在中央服务器上上载、重命名、删除和查看文件。广告用户可以远程和本地访问此服务。我们使用的是Windows身份验证(不是Kerberos) 我已经编写了一些代码来检查NTFS的读写权限(代码在文章的底部)。它只显示他们有读取数据访问权限的文件夹,并隐藏他们没有写入数据访问权限的文件夹的上载/编辑/删除选项。当我在我的机器上运行localhost时,它工作得很好问题是当我发布到QA服务器时,我获得了服务的权限,而不是远程用户的权限。下面的文章解释了发生这种情况的原因。不幸的是,我们现在不能使用Kerberos身份验证 我几乎没有什么想法了。有没有我没有考虑的选项? 通过Windows身份验证使用模拟 通过使用模拟,ASP.NET应用程序可以使用经过身份验证的用户的身份或固定的Windows身份执行代码或访问资源通常在启用模拟时创建的标准模拟级别模拟令牌只允许访问本地资源。要能够访问远程网络资源,需要代理级别的令牌。要在模拟时生成委托级别令牌,您需要使用Kerberos身份验证,并且需要在Active Directory中将您的流程帐户标记为受信任的委托 我在我的Web.config中尝试了C# Asp.net web服务需要返回Windows主体的NTFS文件权限,而不是服务本身,c#,.net,asp.net-mvc,windows-authentication,ntfs,C#,.net,Asp.net Mvc,Windows Authentication,Ntfs,我们正在运行一个ASP.net web服务,该服务需要能够根据用户或其所属广告组的NTFS权限在中央服务器上上载、重命名、删除和查看文件。广告用户可以远程和本地访问此服务。我们使用的是Windows身份验证(不是Kerberos) 我已经编写了一些代码来检查NTFS的读写权限(代码在文章的底部)。它只显示他们有读取数据访问权限的文件夹,并隐藏他们没有写入数据访问权限的文件夹的上载/编辑/删除选项。当我在我的机器上运行localhost时,它工作得很好问题是当我发布到QA服务器时,我获得了服务的权
,但出现以下错误System.Data.SqlClient.SqlException:用户“NT AUTHORITY\ANONYMOUS LOGON”登录失败。
我的下一次尝试是仅在控制器上的HttpGet方法中进行临时模拟
IIdentity WinId = User.Identity;
WindowsIdentity wi = (WindowsIdentity)WinId;
WindowsImpersonationContext wic = wi.Impersonate();
try
{ //GET Method of ActionResult
var user = WindowsIdentity.GetCurrent().User;
var userName = user.Translate(typeof(System.Security.Principal.NTAccount)).ToString();
}
检索用户名表明我可以成功模拟用户,但我仍然只有该服务的文件权限
这是我检查文件夹级访问的NTFS权限的代码
public static bool CheckPermission(string path, string accessType)
{
bool hasReadAccess = false;
bool hasWriteAccess = false;
try
{
DirectoryInfo dir = new DirectoryInfo(path);
DirectorySecurity acl = dir.GetAccessControl();
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(SecurityIdentifier));
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
foreach (AuthorizationRule rule in rules)
{
FileSystemAccessRule fsAccessRule = rule as FileSystemAccessRule;
if (fsAccessRule == null)
continue;
if (accessType.ToLower() == "readdata")
{
return hasReadAccess = CheckReadAccess(fsAccessRule, principal);
}
if (accessType.ToLower() == "writedata")
{
return hasWriteAccess = CheckWriteAccess(fsAccessRule, principal);
}
}
return hasReadAccess;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
这是我的CheckReadAccess方法。CheckWriteAccess本质上是相同的
public static bool CheckReadAccess(FileSystemAccessRule fsAccessRule, WindowsPrincipal principal)
{
bool hasReadAccess = false;
if ((fsAccessRule.FileSystemRights & FileSystemRights.ReadData) > 0)
{
SecurityIdentifier sID = fsAccessRule.IdentityReference as SecurityIdentifier;
if (sID != null)
{
if (principal.IsInRole(sID))
{
if (fsAccessRule.AccessControlType == AccessControlType.Deny)
{
return hasReadAccess = false;
}
else if (fsAccessRule.AccessControlType == AccessControlType.Allow)
{
return hasReadAccess = true;
}
}
}
}
return hasReadAccess;
}
假设您的QA环境位于具有相同位置和权限的单独服务器上是否安全?这是正确的。我提到的QA服务器是一个承载服务的web服务器,但是它的环境与生产web服务器类似。我们尝试访问的路径和权限位于单独的文件服务器上。谢谢你的回复。我是一个编程新手,但在这篇文章中我被抛到了池的最深处。这篇博客基本上是我们需要做的,没有使用Kerberos。我并不过分乐观,我们会找到一个方法去做的。