【发布时间】:2013-04-02 09:10:42
【问题描述】:
我有一个 ASP.NET 应用程序,它使用项目目录中的一些文件(许可证、日志等)。 我需要检查文件权限的方法。 我写了这个:
public static bool IsCurrentUserHaveAccessToFile(string filePath,
FileSystemRights accessType)
{
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
FileSecurity security = File.GetAccessControl(filePath);
var rights = security.GetAccessRules(true, true, typeof(NTAccount));
foreach (FileSystemAccessRule right in rights)
{
NTAccount ntAccount = right.IdentityReference as NTAccount;
if (principal.IsInRole(ntAccount.Value) &&
right.AccessControlType == AccessControlType.Deny &&
right.FileSystemRights.Contains(accessType))
{
log.Debug(string.Format("Checking user {0} for access permissions {1} to file {2} - DENY", currentUser.Name, accessType, filePath));
return false;
}
}
log.Debug(string.Format("Checking user {0} for access permissions {1} to file {2} - ALLOW", currentUser.Name, accessType, filePath));
return true;
}
这个方法获取一个当前用户并且..好吧,你可以看到它:)
问题是:真的是用来访问文件的用户吗? 如果 IIS 使用了与当前用户不同的其他用户,我如何以编程方式获取它?
【问题讨论】:
标签: asp.net windows iis permissions