我想知道我的用户是否是域名,登录名和密码的活动目录帐户。 我测试了这个功能:
public void ValidateCredentials(string sUserName, string sPassword)
{
PrincipalContext adContext = new PrincipalContext(ContextType.Domain);
using (adContext)
{
bool tes= adContext.ValidateCredentials(sUserName, sPassword);
}
}
但即使是域名,用户和密码也是错误的,我总是返回。
答案 0 :(得分:0)
我个人使用此代码通过P / Invoke执行此操作,我从来没有遇到任何问题:
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
public enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9,
}
public enum LogonProvider
{
LOGON32_PROVIDER_DEFAULT = 0,
LOGON32_PROVIDER_WINNT35 = 1,
LOGON32_PROVIDER_WINNT40 = 2,
LOGON32_PROVIDER_WINNT50 = 3
}
public static bool LdapAuthentication(string domain, string user, string password, Configurator cfg)
{
try
{
LogonType lt = LogonType.Network;
IntPtr token = IntPtr.Zero;
return (LogonUser(user, domain, password, (int)lt, (int)LogonProvider.LOGON32_PROVIDER_DEFAULT, out token));
}
catch
{
return (false);
}
}
如果您仍有问题,也许您可以尝试更改LogonType
。
如果回答问题,请将此答案标记为已接受。
答案 1 :(得分:0)
或者,您可以试试这个......
try
{
var credentials = new NetworkCredential(sUserName, sPassword);
using (var connection = new LdapConnection(domainName))
{
connection.AuthType = AuthType.Kerberos
connection.Bind(credentials);
}
return true;
}
catch
{
//handle errors as you see fit
return false;
}