【发布时间】:2010-11-30 15:45:56
【问题描述】:
我在通过 System.DirectoryServices 从 Active Directory 获取组时遇到问题
最初我在域中注册的计算机上启动我的应用程序,但由于它是一个活动域,我不想对 AD 进行任何写入,所以我设置了一台 Windows XP 作为计算机主机操作系统,并在虚拟机上安装了 windows server 2003。
我在机器中添加了另一个以太网端口并设置了一个交换机,1个以太网端口专用于VM,另一个端口用于主机。
在配置 IP 地址以使它们进行通信后,我将我的应用程序转移到主机上并启动它,但我得到了一个 DirectoryServicesCOMException。
提示用户名和密码无效 :( 只是为了检查它不是活动目录,我创建了第三台虚拟机并安装了 Windows XP,我使用在 APP 中测试的凭据将其添加到域中,工作一种享受。
所以我认为这一定是因为运行应用程序的机器不是域的一部分。
这是导致问题的代码块:
public CredentialValidation(String Domain, String Username, String Password, Boolean Secure)
{
//Validate the Domain!
try
{
PrincipalContext Context = new PrincipalContext(ContextType.Domain, Domain); //Throws Exception
_IsValidDomain = true;
//Test the user login
_IsValidLogin = Context.ValidateCredentials(Username, Password);
//Check the Group Admin is within this user
//******HERE
var Results = UserPrincipal.FindByIdentity(Context, Username).GetGroups(Context);
foreach(Principal Result in Results)
{
if (Result.SamAccountName == "Domain Admins")
{
_IsAdminGroup = true;
break;
}
}
Results.Dispose();
Context.Dispose();
}
catch (PrincipalServerDownException)
{
_IsValidDomain = false;
}
}
登录对话框中的信息是这样输入的:
Domain: test.internal
Username: testaccount
Password: Password01
希望有人能解释一下这个错误。
更新:
检查服务器上的安全日志后,我可以看到我的登录尝试是成功的,但这归结为:
_IsValidLogin = Context.ValidateCredentials(Username, Password);
我检查组之后的行导致错误,所以主要问题是下面的代码行在未加入网络的机器上无法正常工作: p>
var Results = UserPrincipal.FindByIdentity(Context, Username).GetGroups(Context);
【问题讨论】:
-
您确定该方法输入的用户凭据对于您尝试对给定用户进行身份验证的给定域是正确的吗?这个用户真的存在吗?您是否尝试过使用
System.DirectoryServices命名空间中的DirectoryEntry和DirectorySearcher类的老式方法?只是建议,我不太了解 Principal 对象。 -
用户存在,因为我在网络上放置了另一台具有相同凭据的 VM XP 机器,我可以在 AD 中看到该用户,并通过在 cmd 中运行命令
start \\server1我得到凭据对话框和能够通过它进行身份验证,这在属于网络的机器上 100% 有效。
标签: c# active-directory active-directory-group