【问题标题】:impersonating C# code in windows 7在 Windows 7 中模拟 C# 代码
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我使用了相同的代码...看起来我模拟的用户没有足够的权限..
【解决方案2】:

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

在使用默认 apppool 身份运行时,我在 asp.net 应用程序中收到了完全相同的消息。将 apppool 用户更改为具有“域管理员”权限的域用户解决了这个问题(也适用于 Windows 7)。

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多