SORU
11 EYLÜL 2009, Cuma


C# eğer kullanıcı bir klasöre yazma erişimi varsa Test

Eğer bir kullanıcı aslında bunu denemeden önce bir klasöre yazabilirsiniz eğer test etmem lazım.

Klasörü Directory.GetAccessControl() yöntem kullanarak güvenlik izinleri almaya çalışan aşağıdaki yöntemi (C# 2.0) uygulanan ettik.

private bool hasWriteAccessToFolder(string folderPath)
{
    try
    {
        // Attempt to get a list of security permissions from the folder. 
        // This will raise an exception if the path is read only or do not have access to view the permissions. 
        System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return false;
    }
}

Yazma erişimi için test etmek için nasıl araştırıyordum ne zaman böyle bir şey geldi ve aslında Windows izinleri test etmek için çok karmaşık göründü. Aşırı basitleştirme şeyler duyuyorum ve bu yöntem işe yarıyor gibi olmasa da sağlam olduğunu düşünüyorum.

Eğer geçerli kullanıcı yazma erişimi varsa test etmek için benim yöntem doğru çalışır mı?

CEVAP
22 Mart 2011, Salı


Bu biraz bu yazı için geç olduğunun farkındayım, ama bu kod biraz faydalı olabilir.

string path = @"c:\temp";
string NtAccountName = @"MyDomain\MyUserOrGroup";

DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));

//Go through the rules returned from the DirectorySecurity
foreach (AuthorizationRule rule in rules)
{
    //If we find one that matches the identity we are looking for
    if (rule.IdentityReference.Value.Equals(NtAccountName,StringComparison.CurrentCultureIgnoreCase))
    {
        //Cast to a FileSystemAccessRule to check for access rights
        if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData)>0)
        {
            Console.WriteLine(string.Format("{0} has write access to {1}", NtAccountName, path));
        }
        else
        {
            Console.WriteLine(string.Format("{0} does not have write access to {1}", NtAccountName, path));
        }
    }
}

Console.ReadLine();

İçine bir uygulama Konsol ve eğer ihtiyacınız ne olursa bakın o damla.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Myron and Nejusha dance

    Myron and Ne

    2 AĞUSTOS 2012
  • Sean Murphy

    Sean Murphy

    4 ŞUBAT 2009
  • xiaoyu85

    xiaoyu85

    20 ŞUBAT 2010