Я хочу перечислить все файлы в папке, к которым у пользователя есть доступ для чтения. Пользователь посещает веб-сайт и может использовать проверку подлинности с помощью форм для некоторых аспектов сайта (например, добавления ссылок и т. д.), но я хочу перечислить файлы в данной папке, используя свои учетные данные Windows (поскольку у меня отключен анонимный доступ), скрывая те, которые они не могу читать.
Однако при использовании Directory.GetFiles
он также включает файлы, которые нельзя прочитать (хотя метаданные (размер файла, дата создания и т. д.) могут быть прочитаны).
Вот что у меня есть:
string[] files;
string[] folders;
string rootDir = @"\\server\path\to\dir\";
WindowsIdentity id = (WindowsIdentity)User.Identity ;
using (System.Security.Principal.WindowsImpersonationContext context = System.Security.Principal.WindowsIdentity.Impersonate(id.Token))
{
files = Directory.GetFiles(rootDir);
folders = Directory.GetDirectories(rootDir);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
FileSecurity fs = fi.GetAccessControl(AccessControlSections.Access);
//foreach (FileSystemAccessRule rule in fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)))
//{
// Response.Write((FileSystemRights.Read & rule.FileSystemRights) + " <br />");
//}
Response.Write(file + " " + fi.Length + "<br />");
}
context.Undo();
}
Когда я посещаю страницу, я получаю UnauthorizedAccessException
, как только я использую GetAccessControl
, даже если он должен использовать учетные данные текущего пользователя. Отключить using
не удается, поскольку учетная запись asp.net не имеет доступа к папке. Когда FileSecurity
закомментирован, он перечисляет все файлы.
Трассировки стека:
[UnauthorizedAccessException: Attempted to perform an unauthorized operation.]
System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd) +697
System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext) +63
System.Security.AccessControl.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory) +86
System.Security.AccessControl.FileSecurity..ctor(String fileName, AccessControlSections includeSections) +42
System.IO.FileInfo.GetAccessControl(AccessControlSections includeSections) +29
UNCDirectory.Page_Load(Object sender, EventArgs e) +213
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Любые идеи о том, как я могу это сделать, не прибегая к попытке открыть каждый файл и поймать возникающее исключение?