Using directives
namespace Impersonal
{
class Program
{
测试代码
[DllImport("advapi32.dll", SetLastError = true)]
public extern static bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
public delegate void ImpersonatedWork();
/// <summary>
/// 以指定用户的身份去做一件事情
/// </summary>
/// <param name="UserName"></param>
/// <param name="PWD"></param>
/// <param name="WhatToDo"></param>
public static void ImpersonateAndDo(string UserName, string PWD, ImpersonatedWork WhatToDo)
{
扮演用户
以新用户身份调用
取消扮演
}
}
}
namespace Impersonal
{
class Program
{
测试代码
[DllImport("advapi32.dll", SetLastError = true)]
public extern static bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
public delegate void ImpersonatedWork();
/// <summary>
/// 以指定用户的身份去做一件事情
/// </summary>
/// <param name="UserName"></param>
/// <param name="PWD"></param>
/// <param name="WhatToDo"></param>
public static void ImpersonateAndDo(string UserName, string PWD, ImpersonatedWork WhatToDo)
{
扮演用户
以新用户身份调用
取消扮演
}
}
}
最近太忙,好久不来,贴段刚写的代码,希望大家还记得我.