在 Windows 7 中模拟 C# 代码

发布于 2024-11-18 18:11:40 字数 1566 浏览 3 评论 0原文

我需要模拟我的代码以使用不同的 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 

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

I need to impersonate the my code to run with a different windows user id, right now i am using some code which i got from web which is calling some native libraries.

the purpose of the code is to change the permissions on a file server user directories permissions.

I can change my folder permissions, but i have the credentials of the other user to go and change the permissions on his folder. But i am running the code on my machine by impersonating him.

But, i am getting un authorized exception.

the code i am using is:

[DllImport("advapi32.dll", SetLastError = true)]
        private static extern int LogonUser(
            string lpszUserName,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);

I am not sure this is working in Windows 7 or not. Is there any one who faced any issue like this..?

Exception i am getting:

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 

Could you please share some solutions..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情栀口红 2024-11-25 18:11:40

查看WindowsIdentity.Impersonate。在那里您将看到如何操作的示例。

Have a look at WindowsIdentity.Impersonate. There you'll see an example of how to go about.

音盲 2024-11-25 18:11:40

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

当使用默认的应用程序池身份运行时,我在 ASP.NET 应用程序中尝试此代码时得到了完全相同的消息。将应用程序池用户更改为具有“域管理员”权限的域用户解决了此问题(Windows 7 也是如此)。

Are you using a domain joined machine with ASP.NET?

I got the exact same message tring this code in a asp.net application when running with the default apppool identity. Changing the apppool user to a domain user with 'domain admin' permissions solved this problem (also Windows 7).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文