사용자가 읽기 권한이있는 폴더의 모든 파일을 나열하려고합니다. 사용자가 웹 사이트를 방문하고 사이트의 일부 측면 (예 : 링크 추가 등)에 양식 인증을 사용할 수 있지만 익명 액세스가 해제 된 이후로 Windows 자격 증명을 사용하여 특정 폴더의 파일을 나열하고 숨길 수 있습니다 읽을 수 없습니다.사용자에게 읽기 권한이있는 파일 나열 (ASP.NET)
그러나 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();
}
, 나는 그것이 현재 사용자의 자격 증명을 사용해야에도 불구하고, 최대한 빨리 GetAccessControl
을 사용뿐만 UnauthorizedAccessException
를 얻을. 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
어떤 아이디어?
나는 nitpick ...을 싫어하지만 왜 goto와 라벨을 사용하고 있습니까? 필요한 경우 휴식하거나 계속하십시오. –
시도해 보았지만 FileAccessRule foreach만을 깨뜨린 것입니다.이 파일에서 벗어나 다음 파일로 계속 진행하려고했습니다. 나는 bool이 사용될 수 있다고 생각하지만 ... 라벨은 사용하기 좋지 않다. 그렇다면 왜 안 되니? – SamWM
Goto는 중첩 루프를 제거하는 데 유용합니다. 각 루프 수준에서 불필요한 조건부 중단을 피함으로써 많은 경우 논리를 단순화하고 성능을 향상시킵니다. – Triynko