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);
相关文章: