以不同的用户身份运行代码(C#)
答
模拟需要调用一些原生API(即,LogonUser的),所以它可能不值得发布3页包装代码。此页面有一个完整的工作示例:http://platinumdogs.wordpress.com/2008/10/30/net-c-impersonation-with-network-credentials/
请注意,模拟具有重要的安全性考虑因素。确保您遵循最佳做法。
答
这article解释它很简洁:
下面是文章的代码片段:
IntPtr accessToken = IntPtr.Zero;
....
//You have to initialize your accessToken with API calling
....
WindowsIdentity identity = new WindowsIdentity(accessToken);
WindowsImpersonationContext context = identity.Impersonate();
...
// Now your code is using the new WindowsLogin and you can do what ever this login can do
...
//Now you can return to your current login of Windows
context.Undo();
答
是模仿有助于以不同的用户运行一些代码。它在我的情况下工作正常。 (感谢米兰Matějka)
我还发现了一个Ref链接。希望它可以帮助您轻松地从nuget获得包装: http://iamfixed.blogspot.de/2017/11/run-code-as-different-user-in-c-from.html
文章中的代码已不存在。 – 2016-12-02 14:58:42