问题描述
我需要在活动目录中验证特定用户的路径,以便让他对我的网站进行身份验证。有没有一种方法可以让我用户获取完整路径。我已经深入研究了 PrincipalSearch,但我认为这不适用于特定用户,它会搜索完整的 AD。 这是我目前所拥有的:
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain,ip,container,user,pass);
UserPrincipal userAD = UserPrincipal.FindByIdentity(principalContext,username);
if (userAD != null)
{
if (principalContext.ValidateCredentials(username,password))
{
return true;
}
else
{
return false;
}
我想为在 userAD 变量中使用的用户获取 Active Directory 中的完整路径。我该怎么做?有什么建议吗?
解决方法
我使用了 DistinguishedName 方法来获取用户的位置。代码现在是这样的:
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain,ip,container,user,pass);
UserPrincipal userAD = UserPrincipal.FindByIdentity(principalContext,username);
var path = userAD.DistinguishedName;
if (userAD != null)
{
if(path.Contains("blabla")){
if (principalContext.ValidateCredentials(username,password))
{
return true;
}
else
{
return false;
}
}
DistinguishedName 返回 userAD 中指定的用户的完整路径。希望这有用!