在Windows 7中模拟C#代码

问题描述:

我需要模拟我的代码以不同的Windows用户ID运行,现在我正在使用一些代码,我从网上调用了一些本地库。在Windows 7中模拟C#代码

代码的目的是更改文件服务器上用户目录权限的权限。

我可以更改我的文件夹权限,但我有其他用户的凭据去并更改他的文件夹的权限。但是我通过冒充他来运行我的机器上的代码。

但是,我得到未经授权的例外。

我使用的代码是:

[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 

能否请您分享一些解决方案..?

+0

请详细说明例外情况,应该有所帮助 –

+0

编辑例外信息的情况; – sbmandav

+0

为什么不使用'runas'? –

看一看WindowsIdentity.Impersonate。你会看到一个如何去做的例子。

+0

我使用了相同的代码...看起来像我冒充的用户没有足够的权限.. – sbmandav

您是否在使用ASP.NET加入域的计算机?

当使用默认的应用程序标识运行时,我得到了一个完全相同的消息,将这段代码放在一个asp.net应用程序中。使用'域管理员'权限将应用程序池用户更改为域用户可以解决此问题(还包括Windows 7)。