【问题标题】:Retrieve full name from ldap AD从 ldap AD 检索全名
【发布时间】:2014-11-13 10:50:32
【问题描述】:

这是我用于验证用户身份的 LDAP 代码。用户登录后,我必须显示用户全名。如何从 AD 获取用户的全名?

<?php
FUNCTION ldapCheckLogin ($username, $upasswd) {

    $ldaphost = '10.20.30.40';   
    $ldapport = 389;
    $ds = ldap_connect($ldaphost, $ldapport) 
        or die("Could not connect to our login server!");

    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
    ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);

    if ($ds) 
    {
        //$username = 'na\'; //OK - Congratulations! na\spups is authenticated.
        $upname = 'iap\\' . $username;

        $ldapbind = @ldap_bind($ds, $upname, $upasswd);

        if ($ldapbind) {
            //print "Congratulations! $username is authenticated.<BR><BR>";
            ldap_unbind( $ds ); 
            return true;
        } else { //print "$username  - Access Denied!<BR><BR>";
            return false;
        }
    } else {
        return false;
    }
}

?>

【问题讨论】:

  • 您的某些代码似乎丢失了。你也可以考虑格式化你的代码以获得更好的可读性

标签: php active-directory ldap


【解决方案1】:

您需要使用带有用户samAccountName 的 ldap_search 检索用户的条目,例如(samaccountname=$username)userPrincipalName 例如(userprincipalname=$username . "@" . $domain.com ) 作为过滤器属性。

samaccountname 仅在域中是唯一的,而 userPrincipalName 在整个林中是唯一的。

当您执行 ldap_search 时,您需要在要返回的属性中包含 cndisplayName

如果搜索成功,那么您需要处理结果条目并提取cn 和/或displayName

【讨论】:

  • 我建议您检索 givenName 和 sn 作为您返回的属性。 cn 和 displayName 可能未填充或以您想要的格式填充。这使您可以根据需要显示名称。
  • 同意,我认为 user2989676 可能会抓住所有四个属性并选择最适合他们需要的。 cn 是必需的,但可能没有用户的全名;很难说他们在他们的环境中有什么。我无法确定,但我认为 AD 中的默认值或 displayNamegivenName + " " + sn
猜你喜欢
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 2020-02-28
  • 2013-07-17
相关资源
最近更新 更多