Search user in win2003 AD by samaccountname

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!! ^^
Who is Participating?
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

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
Chris DentPowerShell DeveloperCommented:

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

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.