在Windows线程中模拟其他用户上下文,需要使用WindowsIdentity.Impersonate方法!同时还需要用LogonUser API来获取安全令牌,代码如下:
using System.Runtime.InteropServices;
using System.Security.Principal;
class Program
{
[DllImport("Advapi32.dll")]
static extern bool LogonUser(
string sUserName,
string sDomain,
string sUserPassword,
uint dwLogonType,
uint dwLogonProvider,
out System.IntPtr token);

[DllImport("Kernel32.dll")]
static extern void CloseHandle(System.IntPtr token);

static void Main()
{
System.IntPtr pToken;
if(LogonUser(
"Administrator",
"DomainName",
"Password",
2,
0,
out pToken)){
WindowsIdentity.Imersonate(pToken);//模拟用户
WindowsIdentity id=WindowsIdentity.GetCurrent();
Console.WriteLine(id.Name);
CloseHandle(pToken);

相关文章:

  • 2021-07-14
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2021-07-17
  • 2021-04-10
  • 2021-12-22
  • 2021-09-18
猜你喜欢
  • 2021-06-29
  • 2021-09-03
  • 2022-12-23
  • 2022-01-08
  • 2021-08-24
  • 2022-01-30
  • 2022-12-23
相关资源
相似解决方案