我目前使用 LogonUser() 在我办公室的本地域上验证用户的用户名和密码,它非常适合我需要它做的事情。
自从我开发了应用程序后,我现在需要让它在我的 VPN 上运行。 LogonUser() 似乎无法与远程验证凭据一起使用。或者会吗?是否可以使用 LogonUser() 来验证远程域帐户上的用户凭据?
我在某些地方读到,使用 LOGON32_LOGON_NEW_CREDENTIALS 作为第 4 个参数(登录类型),使用 LOGON32_PROVIDER_WINNT50 作为第 5 个参数(提供商)就可以了。但每次我尝试我总是成功......我可以提供一个 bogas 用户并通过它每次都会起作用:(。
想法?
编辑 - 添加注释
尝试使用此功能,但我不断收到异常,告诉我用户/密码错误。
public bool Win2kCredentialsIsValid(string domain, string username, string password)
{
string adPath = "LDAP://" + domain + "/rootDSE";
DirectoryEntry adRoot = new DirectoryEntry(adPath, domain + "\\" + username, password, AuthenticationTypes.ReadonlyServer);
try
{
object o = adRoot.Properties["defaultNamingContext"];
}
catch
{
return false;
}
return true;
}
-- 编辑 - 添加更多注释
好的,所以我尝试了另一个例子只是为了让它工作并开始沿着这条路走下去,有几件事需要注意......
- MyServerHostName 就是我服务器的主机名。例如:'Server01'。
- 我在这个例子中的域名是'MyDomain.local'
- 这样我的服务器“Server01.MyDomain.local”的 FQN 就变成了
我试图完成这项工作,但出现了以下错误...
The supplied context type does not match the server contacted. The server type is Domain.
错误出现在:var context = new PrincipalContext(ContextType.ApplicationDirectory, "MyServerHostName:389", "DC=MyDomain,DC=local"))
private bool CheckADCredentials()
{
bool bResults;
using (var context = new PrincipalContext(ContextType.ApplicationDirectory,
"MyServerHostName:389",
"DC=MyDomain,DC=local"))
{
var username = "firstname.lastname";
var email = "firstname.lastname@MyServerHostName";
var password = "123456";
var user = new UserPrincipal(context)
{
Name = username,
EmailAddress = email
};
user.SetPassword(password);
user.Save();
if (context.ValidateCredentials(username, password, ContextOptions.SimpleBind))
{
bResults = true;
}
else
{
bResults = false;
}
user.Dispose();
}
return bResults;
}
最佳答案
我最终采用了不同的解决方案。我没有尝试在我的 PC 未连接到的域上验证用户帐户,而是最终将我的域凭据缓存在数据库中,并构建了一个加盐的 MD5 类型加密函数,这样它就很难……呃……对某些人来说破解它。 ;)
现在我只是在远程工作时根据数据库中缓存的凭据进行验证...它只需要用户先登录域,然后用户就可以日夜远程使用它。 ;)
谢谢!
关于c# - 远程验证用户的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539037/