如何让LDAP返回登录用户的全名? [英] How do I get LDAP to return the logon user fullname?

查看:66
本文介绍了如何让LDAP返回登录用户的全名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中应该返回登录用户的全名。它在我的其他应用程序中工作,但对于下一个应用程序,它一直返回0.我已添加对activeds libraby的引用并导入它。请帮忙。



我这样称呼函数:

I have the following code below that should retuurn the fullname of the user logged on. It worked in my other application but for this next application it keeps returning 0. I have added reference to activeds libraby and imported it. Please help.

I call the function like this :

Dim strFullName As String = GetRealNameFromAd(User.Identity.Name)





这是功能:



This is is the function:

Private Function GetRealNameFromAd(ByVal UsernameToFind As String) As String

      Using searcher As New DirectorySearcher(New DirectoryEntry())
          searcher.PageSize = 1000
          searcher.SearchScope = SearchScope.Subtree
          searcher.Filter = "(&(samAccountType=805306368)(sAMAccountName=" & UsernameToFind & "))"
          Using Results As SearchResultCollection = searcher.FindAll

                              If Results Is Nothing OrElse Results.Count <> 0 Then
                  Throw New ApplicationException("Invalid number of results returned - either no users were found or more than one user account was found")
              End If
              Using UserDE As DirectoryEntry = Results(0).GetDirectoryEntry
                  Return CStr(UserDE.Properties("givenName").Value) & " " & CStr(UserDE.Properties("sn").Value)
              End Using
          End Using
      End Using

  End Function

推荐答案

你得到的错误是什么?我可以看到以下 If 语句中的一个问题。当我将其更改为 Results.Count = 0 时,它对我来说很好。



更改如果结果无效OrElse Results.Count<> 0然后如果结果是什么OrElse Results.Count = 0那么,它完全正常。
What's the error are you getting? I can see one problem with the following If statement. When I changed it to Results.Count = 0, it worked fine for me.

Changed If Results Is Nothing OrElse Results.Count <> 0 Then to If Results Is Nothing OrElse Results.Count = 0 Then, and it worked perfectly fine.


这篇关于如何让LDAP返回登录用户的全名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆