Search user in win2003 AD by samaccountname

Posted on 2005-04-17
Last Modified: 2008-01-09
To whom it may concerned:
My question is about windows 2003 Active Directory.
Now I need to write a program for users to change their AD password using ASP(Active Server Page).
Since we display users' Chinese name in AD, For example, the distinguishname of ldap is : CN=黃黃,OU=MIS,OU=BioIT,DC=ngc,DC=sinica
and the samaccountname is : cccc
User only know their samaccountname, but the program need the find user by distinguishname.
How can I deal with this problem?
Here is some of my code:
Set RootDSE = GetObject("LDAP://RootDSE")
DomainContainer = RootDSE.Get("DefaultNamingContext")
Set UsrObj = GetObject("LDAP://CN=" & UserName & "," & SubContainer & "," & DomainContainer & "")
UsrObj.ChangePassword OldPwd,NewPwd

I would like to know :
Are there any solution that this line
Set UsrObj = GetObject("LDAP://CN=" & UserName & "," & SubContainer & "," & DomainContainer & "") search by users'  samaccountname?
Because user only know their samaccountname, and it is different from UserName(var).

Are there any solution that you can give me?
Thank you so much!! ^^
Question by:chjc
    LVL 70

    Accepted Solution


    This probably isn't the most efficient method of doing this... but it does work...

    Option Explicit

    ' Variable Declaration

    Dim objRootDSE, objDomainRoot, objItem
    Dim strSAMName, strPassword

    ' strSAMName and strPassword should be passed into this script.

    strSAMName = "SomeSAMAccountName"
    strPassword = "SomePassword"

    ' Connect to AD Root

    Set objRootDSE = GetObject("LDAP://rootDSE")
    Set objDomainRoot = GetObject("LDAP://" & objRootDSE.Get("defaultNamingContext"))

    Sub ProcessUsers(objUsers)

    ' This sub processes the users for a set OU

          Dim objUser
          Dim strUserName

          objUsers.Filter = Array("user")
          For Each objUser In objUsers
                strSAMName = LCase(strSAMName)
                strUserName = LCase(objUser.sAMAccountName)
                If (strSAMName = strUserName) Then
                      objUser.SetPassword strPassword
                End If
    End Sub

    Sub OURecurse(objFirst)

    ' This OU is responsible for going through the AD Structure

          Dim objOU

          Set objOU = GetObject(objFirst.ADSPath)
          For Each objItem in objOU
                If (objItem.Class = "organizationalUnit") Then
                      ProcessUsers objItem
                      OURecurse objItem
                End If
    End Sub

    ' Main Code section begins here

    ' Objects like the Users OU is actually a container so it must be caught with the container check
    ' Other objects we're interested in are real OUs
    ' Not recursing through container objects

    For Each objItem in objDomainRoot
          If (objItem.Class = "container") Then
                ProcessUsers objItem
          End If
          If (objItem.Class = "organizationalUnit") Then
                ProcessUsers objItem
                OURecurse objItem
          End If
    LVL 70

    Expert Comment

    by:Chris Dent

    Just in case it's still being viewed I do have a much better solution for this. Takes the Username and returns the Distinguished Name.

    Const ADS_NAME_TYPE_1779 = 1
    Const ADS_NAME_TYPE_NT4 = 3

    Dim objNetwork, objNameTranslate, objOU
    Dim strUserName, strDN

    Set objNetwork = CreateObject("WScript.Network")
    strUserName = objNetwork.UserDomain & "\" & strUserName

    Set objNameTranslate = CreateObject("NameTranslate")
    objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
    objNameTranslate.Set ADS_NAME_TYPE_NT4, strUserName
    strDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)

    Set objNameTranslate = Nothing
    Set objNetwork = Nothing


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now