قائمة الملفات التي يمتلك المستخدم حق الوصول إليها للقراءة (ASP.NET)

StackOverflow https://stackoverflow.com/questions/2209264

سؤال

أرغب في سرد ​​جميع الملفات الموجودة في المجلد الذي يتمتع المستخدم بإمكانية الوصول إليه للقراءة.يزور المستخدم موقع الويب ويمكنه استخدام مصادقة النماذج لبعض جوانب الموقع (على سبيل المثال.إضافة روابط وما إلى ذلك)، ولكني أرغب في إدراج الملفات في مجلد معين باستخدام بيانات اعتماد 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 off فشل لأن حساب 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

هل هناك أي أفكار حول كيفية القيام بذلك، دون اللجوء إلى محاولة فتح كل ملف والتقاط الاستثناء الذي يحدث؟

هل كانت مفيدة؟

المحلول

تمكنت من إصلاحه.عندما ترفض قراءة ملف ما، فإنه يرفض أيضًا أذونات القراءة، ومن ثم UnauthorizedAccessException.لذلك باستخدام try... catch ... يبدو أن هذا هو الخيار الوحيد.

من الممكن منع الاستثناء إذا تم رفض القراءة والسماح بـ ReadPermissions (يتم ذلك ضمن الإعدادات المتقدمة).هذا يعالج كلاً من:

string[] files;
string[] folders;
WindowsIdentity id = (WindowsIdentity)User.Identity;
using (System.Security.Principal.WindowsImpersonationContext context = id.Impersonate())
{
    files = Directory.GetFiles(RootDir);
    folders = Directory.GetDirectories(RootDir);

    foreach (string file in files)
    {
        FileInfo fi = new FileInfo(file);
        FileSecurity fs = null;
        try
        {
            fs = fi.GetAccessControl(AccessControlSections.Access);
        }
        catch (UnauthorizedAccessException)
        {
            goto Next;
        }
        foreach (FileSystemAccessRule rule in fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)))
        {
            if (id.User.CompareTo(rule.IdentityReference as SecurityIdentifier) == 0)
            {
                if(rule.AccessControlType.ToString() == "Deny" &&
                    rule.FileSystemRights.ToString().Contains("ReadData"))
                {
                    goto Next;
                }
            }
        }

        Response.Write(file + " " + fi.Length + "<br />");
        // next in sequence. label for goto
        Next: ;
    }
    context.Undo();
}

يمكن الآن إدراج الملفات في أي دليل (باستخدام الانتحال) دون منح حساب ASP.NET (عادةً خدمة الشبكة) إمكانية الوصول أيضًا.

نصائح أخرى

ربما تحتاج إلى إعداد مصادقة Windows/المتكاملة في asp.net/IIS. http://msdn.microsoft.com/en-us/library/aa292114(VS.71).aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top