Эффективные права
От: bpost  
Дата: 05.07.09 12:32
Оценка:
Уважаемый All. Есть ли возможность используя C# получить эффективные (действующие) права пользователя (группы пользователей) на сетевую папку ?
Re: Эффективные права
От: stump http://stump-workshop.blogspot.com/
Дата: 05.07.09 15:52
Оценка:
Здравствуйте, bpost, Вы писали:

B>Уважаемый All. Есть ли возможность используя C# получить эффективные (действующие) права пользователя (группы пользователей) на сетевую папку ?


См. класс System.Security.AccessControl.DirectorySecurity
Понедельник начинается в субботу
Re[2]: Эффективные права
От: Аноним  
Дата: 07.07.09 04:19
Оценка:
Здравствуйте, stump, Вы писали:

S>Здравствуйте, bpost, Вы писали:


B>>Уважаемый All. Есть ли возможность используя C# получить эффективные (действующие) права пользователя (группы пользователей) на сетевую папку ?


S>См. класс System.Security.AccessControl.DirectorySecurity


Вы не могли бы уточнить, как правильно воспользоваться в данной ситуации? У меня следующий код даёт не то

            string NetDirName = @"\\ServerName\ShareName";
//            string NetDirName = @"D:\Temp";
            DirectoryInfo NetDirInfo = new DirectoryInfo(NetDirName);

            AuthorizationRuleCollection Rules = null;
            try
            {
                
                DirectorySecurity NetDirSec = NetDirInfo.GetAccessControl(AccessControlSections.Access);
                Rules = NetDirSec.GetAccessRules(true, true, typeof(SecurityIdentifier));
//                Rules = NetDirSec.GetAccessRules(true, true, typeof(NTAccount));
            }
            catch(UnauthorizedAccessException E)
            {
                Console.WriteLine("Exception: {0}\n", E.Message);
            }
            catch(Exception E)
            {
                Console.WriteLine("Exception: {0}\nMessage: {1}\n", E.GetType().Name, E.Message);
            }

            if (Rules != null)
            {
                Console.WriteLine("OK");
                foreach (FileSystemAccessRule AR in Rules)
                {
                    IdentityReference IR = AR.IdentityReference;
                    IdentityReference Account = IR.Translate(typeof(NTAccount));
                    Console.WriteLine(IR.Value);
                    Console.WriteLine(Account.Value + "\n");
                    // Console.WriteLine(Account.ToString());
                    IR = null;
                    Account = null;
               }
           }

На выходе получается DACL не сетевой шары, а объекта файловой системы, на котором она базируется.
Re[3]: Эффективные права
От: Аноним  
Дата: 07.07.09 04:55
Оценка:
Вот так вроде как получается то, что надо

    class NetDirectorySecurity : NativeObjectSecurity
    {
        public NetDirectorySecurity(string ResName, AccessControlSections IncludeSections)
            : base(true, ResourceType.LMShare, ResName, IncludeSections)
        {
        }

        private static FileSystemRights RightsFromAccessMask(int accessMask)
        {
            return (FileSystemRights)accessMask;
        }

        public sealed override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, 
            bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
        {
            return new FileSystemAccessRule(identityReference, RightsFromAccessMask(accessMask), inheritanceFlags, propagationFlags, type);
        }

        public sealed override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, 
            bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
        {
            return new FileSystemAuditRule(identityReference, RightsFromAccessMask(accessMask), inheritanceFlags, propagationFlags, flags);
        }

        public override Type AccessRightType { get { return typeof(FileSystemRights); } }
        public override Type AccessRuleType { get {return typeof(FileSystemAccessRule); } }
        public override Type AuditRuleType { get  { return typeof(FileSystemAuditRule); } }
    }

// Программа

            string NetDirName = @"\\ServerName\ShareName";

            AuthorizationRuleCollection Rules = null;
            try
            {                
                NetDirectorySecurity NDS = new NetDirectorySecurity(NetDirName, AccessControlSections.Access);
                Rules = NDS.GetAccessRules(true, true, typeof(SecurityIdentifier));
            }
            catch(UnauthorizedAccessException E)
            {
                Console.WriteLine("Exception: {0}\n", E.Message);
            }
            catch(Exception E)
            {
                Console.WriteLine("Exception: {0}\nMessage: {1}\n", E.GetType().Name, E.Message);
            }

            if (Rules != null)
            {
                Console.WriteLine("OK");
                foreach (FileSystemAccessRule AR in Rules)
                {
                    IdentityReference IR = AR.IdentityReference;
                    IdentityReference Account = IR.Translate(typeof(NTAccount));
                    Console.WriteLine(IR.Value);
                    Console.WriteLine(Account.Value + "\n");
                    IR = null;
                    Account = null;
               }
           }


Но ввиду полного отсутсвия опыта в NET, я не знаю, насколько это правильно.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.