【发布时间】:2011-09-28 16:06:08
【问题描述】:
我需要模拟我的代码以使用不同的 Windows 用户 ID 运行,现在我正在使用从 Web 获取的一些代码,它正在调用一些本机库。
代码的目的是更改文件服务器用户目录的权限。
我可以更改我的文件夹权限,但我有其他用户的凭据去更改他的文件夹的权限。但是我通过冒充他在我的机器上运行代码。
但是,我得到了未经授权的异常。
我使用的代码是:
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
我不确定这是否适用于 Windows 7。有没有人遇到过这样的问题..?
我得到的异常:
Messagee:"{"Attempted to perform an unauthorized operation."}"
stack trace:
at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections)
at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)
at System.IO.Directory.SetAccessControl(String path, DirectorySecurity directorySecurity)
at FolderAccessManager.Program.Main(String[] args) in
您能分享一些解决方案吗..?
【问题讨论】:
-
请提供异常的完整细节,应该会有所帮助
-
编辑了带有异常消息的案例;
-
LogonUser不是简单地返回一个令牌,然后您必须以其他方式使用它吗?
标签: c# .net active-directory impersonation