4

我正在使用 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
}
4

0 回答 0