Retrieving username from Active Directory RRS feed

  • Question

  • User671830032 posted
    Yesterday i could not get DirectoryServices to work properly and now that it is i don't know how to retrieve username (Firstname Lastname) from AD. HELP :-) i know it is something like: ldap://cn=txtusername, etc. I can authenticate the user but now i need to retrieve his full name. thanks Hiral
    Friday, October 31, 2003 2:35 PM

All replies

  • User-1227493259 posted
    you can just just context.User.Identity.Name which will return the currently logged in username
    Monday, November 3, 2003 3:51 PM
  • User671830032 posted
    that will return the JDoe user name (logon name) but i want user name to be John Doe Hiral
    Tuesday, November 4, 2003 10:06 AM
  • User-1227493259 posted
    Here is some sample code that I used to get any info from active directory.. including the display name: <%@ Page Language="VB" %> <%@ import Namespace="System" %> <%@ import Namespace="System.Web.Security" %> <%@ import Namespace="System.Security.Principal" %> <%@ import Namespace="System.Web" %> <%@ import Namespace="System.DirectoryServices " %> <%@ import Namespace="System.DirectoryServices.DirectoryEntry " %> <script runat="server"> Public Function GetUserInfo(ByVal inSAM as String, ByVal inType as String) as String ' replace MYdomain with your dmain name, ex: for microsoft.com it would be ' MICROSOFT ' replace DomainExt with COM in the case of microsoft.com Dim sPath as String = "LDAP://DC=MYdomain,DC=DomainExt" Dim SamAccount as String = Right(inSam, Len(inSam) - InStr(inSam, "\")) ' replace Enterprise_Admin withe a user that has enterprise admin privileges ' replace P_Word with the correct password for this user id Dim myDirectory as New DirectoryEntry(sPath, "Enterprise_Admin", "P_Word") Dim mySearcher as New DirectorySearcher(myDirectory) Dim mySearchResultColl as SearchResultCollection Dim mySearchResult as SearchResult Dim myResultPropColl as ResultPropertyCollection Dim myResultPropValueColl as ResultPropertyValueCollection Try 'Build LDAP query mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount &"))") mySearchResultColl = mySearcher.Findall() 'We only expect one user from the search Select Case mySearchResultColl.Count Case 0 Return "Null" Exit Function Case is > 1 Return "Null" Exit Function end select ' Get the search result from the collection mySearchResult = mySearchResultColl.Item(0) ' Get the properties, they contain the usefull info myResultPropColl = mySearchResult.Properties ' Return the requested property.. displayname, mail, etc myResultPropValueColl = myResultPropColl.Item(inType) Return CStr(myResultPropValueColl.Item(0)) Catch ex as System.Exception ' Handle errors End try end function Sub Page_Load(Sender As Object, E As EventArgs) lblUser.Text = GetUserInfo(User.Identity.Name, "displayName") end Sub </script> I hope that helps you...
    Tuesday, November 4, 2003 12:23 PM
  • User1891653316 posted
    Hi Strohj I just tried your code - it looks very useful. But im getting the following error: Compiler Error Message: BC30002: Type 'DirectoryEntry' is not defined. any idea why i'm getting that? doesnt make sense to me seeing as there is this line: Dim myDirectory as New DirectoryEntry(sPath,..... any idea what I should do to get it to work? Thanks Anton
    Thursday, June 17, 2004 9:35 PM
  • User-1917751600 posted
    if you are using web matrix : Do you have <%@ import Namespace="System.DirectoryServices " %> in your aspx file as well as <assemblies> <add assembly="System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </assemblies> in your web.config file? If you are using vs.net You need to add a refernce to system.directoryservices to your project and put Imports System.DirectoryServices at the top of your .aspx.vb file
    Friday, June 18, 2004 2:57 PM
  • User-120824986 posted
    I have tried the CODE too its giving error :- DirectoryEntry' is not defined.! Any help thanks
    Thursday, August 19, 2004 8:50 PM
  • User236755733 posted
    If you're using VS, you need to Add Reference -> .NET tab -> System.DirectoryServices.dll
    Friday, September 10, 2004 1:52 PM
  • User-120824986 posted
    Yeah guess that should be the reason i'm getting such error! Thanks
    Monday, September 13, 2004 12:40 AM
  • User2053319241 posted
    I am using the above code and have checked and rechecked and getting this error, any ideas. System.Runtime.InteropServices.COMException (0x8007054B): The specified domain either does not exist or could not be contacted at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) at System.DirectoryServices.DirectorySearcher.FindAll() at ASP.default_aspx.GetUserInfo(String inSAM, String inType) in c:\inetpub\wwwroot\expensereporting\default.aspx:line 92 Thank you in advance
    Thursday, January 27, 2005 2:17 PM
  • User2053319241 posted
    Figured it out, had to put the domain name in front of the DC=
    Thursday, January 27, 2005 3:00 PM
  • User1070673925 posted



    Your code for getting user properties works great.  Thank you!

    Can you extend the same code you have to be able to write back values into AD?  I need to have the users modify their own attributes in AD.


    Thank you!

    Sunday, August 9, 2009 2:57 PM