Как добавить SeSecurityPrivilege в каталог в Windows 8.1?
Я хочу добавить SeSecurityPrivilege в каталог.
Я нашел эту библиотеку. http://processprivileges.codeplex.com/
Я проверил код ниже в Windows 8.1
static void Main(string[] args)
{
String dirPath = @"C:\Users\İsmail\AppData\Roaming\Microsoft\Crypto\RSA\S-1-5-21-456447968-1492996402-1718433538-1001";
Process process = Process.GetCurrentProcess();
// Get the privileges and associated attributes.
PrivilegeAndAttributesCollection privileges = process.GetPrivileges();
using (new ProcessPrivileges.PrivilegeEnabler(process, Privilege.Security))
{
// Privilege is enabled within the using block.
DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
directorySecurity.SetOwner(WindowsIdentity.GetCurrent().User);
Directory.SetAccessControl(dirPath, directorySecurity);
privileges = process.GetPrivileges();
PrivilegeState state = process.GetPrivilegeState(Privilege.Security);
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
DirectorySecurity DirSec = dirInfo.GetAccessControl(AccessControlSections.All);
}
}
Следующая строка кода вызывает исключение.
DirectorySecurity DirSec = dirInfo.GetAccessControl(AccessControlSections.All);
Результат исключения:
System.Security.AccessControl.PrivilegeNotHeldException was unhandled
HResult=-2147024891
Message=The process does not possess the 'SeSecurityPrivilege' privilege which is required for this operation.
Source=mscorlib
PrivilegeName=SeSecurityPrivilege
StackTrace:
at System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd)
at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity..ctor(Boolean isContainer, ResourceType resourceType, String name, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.DirectorySecurity..ctor(String name, AccessControlSections includeSections)
at System.IO.DirectoryInfo.GetAccessControl(AccessControlSections includeSections)
at TestACLS.Program.Main(String[] args) in c:\Users\İsmail\Desktop\TestACLS\TestACLS\TestACLS\Program.cs:line 72
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: