为什么LogonUser总是返回错误代码1008?

时间:2012-10-18 08:18:48

标签: c# windows security winapi windows-authentication

我正在使用LogonUserA函数来验证在IIS7 / Win2008上运行的ASP.NET 4.0 Web应用程序中的用户。如果用户输入正确的用户名和密码,它运行良好,但是如果用户名或密码错误,LogonUserA总是返回相同的错误代码1008,转换为模糊的消息“尝试引用不存在的令牌”。当然,如果LogonUserA失败,那么就没有有效的令牌,那么这个错误是什么意思呢?是否存在典型登录错误的特定错误代码?我怎么能得到它?

以下是我的代码:

[DllImport("advapi32.dll", SetLastError = true)]
public static extern int LogonUserA(string lpszUserName,
    string lpszDomain,
    string lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);

if (0 != LogonUserA(userName, domain, newPassword, Logon32LogonInteractive, Logon32ProviderDefault, ref token))
{
    // error is always 1008 if user name of password is incorrect.
    int error = Marshal.GetLastWin32Error();

    var exception = new Win32Exception(error);

    // What I got in error log for above Win32Exception is:
    // System.ComponentModel.Win32Exception (0x80004005): An attempt was made to reference a token that does not exist
}

0 个答案:

没有答案
相关问题