我在尝试在桌面应用程序中模拟活动目录用户时遇到问题。每次我使用LogOn API时,结果均为false。
用户和域确实存在,因为我也可以通过DirectoryServices.AccountManagement同一应用程序上的用户进行身份验证。
在Microsoft网站上已经阅读了有关模拟的文档,甚至在堆栈上有一些文章。另外,使用该SimpleImpersonation库的结果相同。
public class Demo
{
private WindowsImpersonationContext impersonationContext = null;
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
private void Enter()
{
try
{
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
string userName = "myValidUser";
string domain = "my.domain.example";
string password …Run Code Online (Sandbox Code Playgroud)