当前位置: 首页>>代码示例>>C#>>正文


C# DirectorySecurity.SetSecurityDescriptorBinaryForm方法代码示例

本文整理汇总了C#中System.Security.AccessControl.DirectorySecurity.SetSecurityDescriptorBinaryForm方法的典型用法代码示例。如果您正苦于以下问题:C# DirectorySecurity.SetSecurityDescriptorBinaryForm方法的具体用法?C# DirectorySecurity.SetSecurityDescriptorBinaryForm怎么用?C# DirectorySecurity.SetSecurityDescriptorBinaryForm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.AccessControl.DirectorySecurity的用法示例。


在下文中一共展示了DirectorySecurity.SetSecurityDescriptorBinaryForm方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAccessControl

        public static DirectorySecurity GetAccessControl(String path, AccessControlSections includeSections)
        {
            var normalizedPath = Path.NormalizeLongPath(Path.GetFullPath(path));
            IntPtr SidOwner, SidGroup, Dacl, Sacl, ByteArray;
            SecurityInfos SecurityInfos =
                Common.ToSecurityInfos(includeSections);

            int errorCode = (int)NativeMethods.GetSecurityInfoByName(normalizedPath,
                (uint)ResourceType.FileObject,
                (uint)SecurityInfos,
                out SidOwner,
                out SidGroup,
                out Dacl,
                out Sacl,
                out ByteArray);

            ThrowIfError(errorCode, ByteArray);

            uint Length = NativeMethods.GetSecurityDescriptorLength(ByteArray);

            byte[] BinaryForm = new byte[Length];

            Marshal.Copy(ByteArray, BinaryForm, 0, (int)Length);

            NativeMethods.LocalFree(ByteArray);
            var ds = new DirectorySecurity();
            ds.SetSecurityDescriptorBinaryForm(BinaryForm);
            return ds;
        }
开发者ID:jerriclynsjohn,项目名称:LongPath,代码行数:29,代码来源:Directory.cs

示例2: GetAccessControl

        public static DirectorySecurity GetAccessControl(String path, AccessControlSections includeSections)
        {
            var normalizedPath = Path.NormalizeLongPath(Path.GetFullPath(path));
            IntPtr sidOwner, sidGroup, dacl, sacl, byteArray;
            var securityInfos = Common.ToSecurityInfos(includeSections);

            var errorCode = (int)NativeMethods.GetSecurityInfoByName(normalizedPath,
                (uint)ResourceType.FileObject,
                (uint)securityInfos,
                out sidOwner,
                out sidGroup,
                out dacl,
                out sacl,
                out byteArray);

            ThrowIfError(errorCode, byteArray);

            var length = NativeMethods.GetSecurityDescriptorLength(byteArray);

            var binaryForm = new byte[length];

            Marshal.Copy(byteArray, binaryForm, 0, (int)length);

            NativeMethods.LocalFree(byteArray);
            var ds = new DirectorySecurity();
            ds.SetSecurityDescriptorBinaryForm(binaryForm);
            return ds;
        }
开发者ID:red-gate,项目名称:LongPath,代码行数:28,代码来源:Directory.cs

示例3: SetFileSystemRights

        //http://www.west-wind.com/weblog/posts/4072.aspx
        public void SetFileSystemRights(string target, string group, FileSystemRights permission, DeploymentResult r)
        {
            if (!IsDirectory(target) && !IsFile(target))
                return;

            var oldSecurity = Directory.GetAccessControl(target);
            var newSecurity = new DirectorySecurity();

            newSecurity.SetSecurityDescriptorBinaryForm(oldSecurity.GetSecurityDescriptorBinaryForm());

            var accessRule = new FileSystemAccessRule(group,
                                                      permission,
                                                      InheritanceFlags.None,
                                                      PropagationFlags.NoPropagateInherit,
                                                      AccessControlType.Allow);
            bool result;
            newSecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out result);

            if (!result)
                r.AddError("Something wrong happened");

            accessRule = new FileSystemAccessRule(group,
                                                  permission,
                                                  InheritanceFlags.ContainerInherit |
                                                  InheritanceFlags.ObjectInherit,
                                                  PropagationFlags.InheritOnly,
                                                  AccessControlType.Allow);

            result = false;
            newSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule, out result);
            if (!result)
                r.AddError("Something wrong happened");

            Directory.SetAccessControl(target, newSecurity);
            if (result)
                r.AddGood("Permissions set for '{0}' on folder '{1}'", group, target);

            if (!result) r.AddError("Something wrong happened");
        }
开发者ID:oriacle,项目名称:dropkick,代码行数:40,代码来源:DotNetPath.cs

示例4: SetSecurityDescriptor

 private void SetSecurityDescriptor(string path, ObjectSecurity sd, AccessControlSections sections)
 {
     byte[] securityDescriptorBinaryForm = sd.GetSecurityDescriptorBinaryForm();
     if (Directory.Exists(path))
     {
         DirectorySecurity directorySecurity = new DirectorySecurity();
         directorySecurity.SetSecurityDescriptorBinaryForm(securityDescriptorBinaryForm, sections);
         Directory.SetAccessControl(path, directorySecurity);
         base.WriteSecurityDescriptorObject(directorySecurity, path);
     }
     else
     {
         FileSecurity fileSecurity = new FileSecurity();
         fileSecurity.SetSecurityDescriptorBinaryForm(securityDescriptorBinaryForm, sections);
         File.SetAccessControl(path, fileSecurity);
         base.WriteSecurityDescriptorObject(fileSecurity, path);
     }
 }
开发者ID:nickchal,项目名称:pash,代码行数:18,代码来源:FileSystemProvider.cs

示例5: ReceiveFileSystemSecurityInformation

        /// <summary>
        /// Gets the security information of specified handle from file system
        /// </summary>
        /// <param name="sidHandle">Handle to get file security information</param>
        /// <returns><see cref="CommonObjectSecurity"/>Result</returns>
        private CommonObjectSecurity ReceiveFileSystemSecurityInformation(out IntPtr sidHandle)
        {
            var zeroHandle = new IntPtr();
            var pSecurityDescriptor = new IntPtr();

            try
            {
                var namedSecInfoResult = Win32SafeNativeMethods.GetNamedSecurityInfo(PathInfo.FullNameUnc, Win32SecurityObjectType.SeFileObject,
                    Win32FileSystemEntrySecurityInformation.OwnerSecurityInformation | Win32FileSystemEntrySecurityInformation.DaclSecurityInformation,
                    out sidHandle, out zeroHandle, out zeroHandle, out zeroHandle, out pSecurityDescriptor);
                var win32Error = Marshal.GetLastWin32Error();
                // Cancel if call failed

                if (namedSecInfoResult != 0)
                {
                    NativeExceptionMapping(PathInfo.FullName, win32Error);
                }

                var securityDescriptorLength = Win32SafeNativeMethods.GetSecurityDescriptorLength(pSecurityDescriptor);
                var securityDescriptorDataArray = new byte[securityDescriptorLength];
                Marshal.Copy(pSecurityDescriptor, securityDescriptorDataArray, 0, (int)securityDescriptorLength);

                CommonObjectSecurity securityInfo;
                if (ContainsFileAttribute(PathInfo.Attributes, FileAttributes.Directory))
                {
                    securityInfo = new DirectorySecurity();
                    securityInfo.SetSecurityDescriptorBinaryForm(securityDescriptorDataArray);
                }
                else
                {
                    securityInfo = new System.Security.AccessControl.FileSecurity();
                    securityInfo.SetSecurityDescriptorBinaryForm(securityDescriptorDataArray);
                }

                return securityInfo;
            }
            finally
            {
                Win32SafeNativeMethods.LocalFree(zeroHandle);
                Win32SafeNativeMethods.LocalFree(pSecurityDescriptor);
            }
        }
开发者ID:i-e-b,项目名称:tinyQuickIO,代码行数:47,代码来源:FileSecurity.cs

示例6: SetSecurityDescriptor

        } // SetSecurityDescriptor

        private void SetSecurityDescriptor(string path, ObjectSecurity sd, AccessControlSections sections)
        {
            var currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE();
            byte[] securityDescriptorBinary = null;

            try
            {
                // Get the binary form of the descriptor.
                PlatformInvokes.EnableTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
                securityDescriptorBinary = sd.GetSecurityDescriptorBinaryForm();
            }
            finally
            {
                PlatformInvokes.RestoreTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
            }

            try
            {
                PlatformInvokes.EnableTokenPrivilege("SeRestorePrivilege", ref currentPrivilegeState);

                // Transfer it to the new file / directory.
                // We keep these two code branches so that we can have more 
                // granular information when we ouput the object type via 
                // WriteSecurityDescriptorObject.
                if (Directory.Exists(path))
                {
                    DirectorySecurity newDescriptor = new DirectorySecurity();
                    newDescriptor.SetSecurityDescriptorBinaryForm(securityDescriptorBinary, sections);
                    new DirectoryInfo(path).SetAccessControl(newDescriptor);
                    WriteSecurityDescriptorObject(newDescriptor, path);
                }
                else
                {
                    FileSecurity newDescriptor = new FileSecurity();
                    newDescriptor.SetSecurityDescriptorBinaryForm(securityDescriptorBinary, sections);
                    new FileInfo(path).SetAccessControl(newDescriptor);
                    WriteSecurityDescriptorObject(newDescriptor, path);
                }
            }
            finally
            {
                PlatformInvokes.RestoreTokenPrivilege("SeRestorePrivilege", ref currentPrivilegeState);
            }
        }
开发者ID:40a,项目名称:PowerShell,代码行数:46,代码来源:FileSystemSecurity.cs


注:本文中的System.Security.AccessControl.DirectorySecurity.SetSecurityDescriptorBinaryForm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。