[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Search user in win2003 AD by samaccountname

Posted on 2005-04-17
Medium Priority
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
  • 2
LVL 71

Accepted Solution

Chris Dent earned 500 total points
ID: 13804563

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 71

Expert Comment

by:Chris Dent
ID: 16153787

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


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 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