C# 实现Domain Login 验证用户权限与Domain Mode读取
1. Domain Login 验证用户权限
public bool ValidateDomainUser(string UserName, string Password, string Domain)
{
bool bValid = false;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, Domain))
{
bValid = context.ValidateCredentials(UserName, Password);
}
return bValid;
}
{
bool bValid = false;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, Domain))
{
bValid = context.ValidateCredentials(UserName, Password);
}
return bValid;
}
2. Domain Mode读取
private void DomainModeCheck(string UserName, string Password, string Domain)
{
{
DirectoryContext oContext = null;
oContext = new DirectoryContext(DirectoryContextType.Domain, Domain, UserName, Password);
DomainMode DM = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(oContext).DomainMode;
MessageBox.Show(DM.ToString());
}
oContext = new DirectoryContext(DirectoryContextType.Domain, Domain, UserName, Password);
DomainMode DM = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(oContext).DomainMode;
MessageBox.Show(DM.ToString());
}
注:本代码经过测试,运行结果正确无误,但是在Windows Server 2003 冗余Domain Servers的环境中当主Domain Server(PDC)断网的情况下,使用从Domain Server (BDC)进行权限验证,ValidateDomainUser()无法对用户权限进行验证,并且有异常抛出。
欢迎朋友们对于此问题进行讨论..