Partager via


Creating a User URL

Creating a User URL

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Storing user information in Active Directory is accomplished by constructing a URL that identifies where the information is stored. The example code in this topic demonstrates one way to construct a user URL. The user URL can then be used to access the user object in Active Directory. If a user object was created using a URL that was not generated by this example code, URLs returned by this example might not correctly address that user object.

Although the example constructs the URL string, it does not create the object in Active Directory. If you use this code, you must ensure the validity of the user name and other parameters passed to the function. Your code must also verify whether the URL returned by this function refers to a valid Active Directory location.

When creating a user object in Active Directory, the Active Directory Service Interfaces (ADSI) application programming interface (API) takes separate parameters for the user name and the container in which to create the new user. To use this sample for creating new Active Directory user objects, you may need to adapt the following sample to return the container path without the user name.

Visual Basic

'/////////////////////////////////////////////////////////////////
'// Name:       CreateUserURL
'// Purpose:    To create a URL for a user
'// Parameters:
'//             strUrl [out]  : Returns the URL for the user or container. For example:
'//             LDAP://subdomain.example.com/CN=User2,CN=Users,DC=subdomain,DC=example,DC=com
'//             strContainer  : Specifies the name of the container that the user exists in.
'//                             If empty, the URL assumes the user is in the domain-level
'//                             container.
'//             IsContainerURL: When True, returns a URL to the container holding the user.
'//                             When False, returns a URL to the user object.
'//             strUserName   : Specifies the name of the user. If this parameter is empty,
'//                             a user name will be created in the form "CDOUser_XXXXX",
'//                             where XXXXX is replaced by a random number between zero and
'//                             33554431.
'//
'////////////////////////////////////////////////////////////////////

Public Sub CreateUserURL(ByRef strURL As String, _
                              ByVal strContainer As String, _
                              ByVal IsContainerURL As Boolean, _
                              Optional ByRef strUserName As String) As Boolean
    On Error Resume Next

    Dim objIADs             As IADs
    Dim iADSSystem          As New ADSystemInfo
    Dim strDomainDN         As String
    Dim strDomainDNSName    As String
    Dim strContainerDN      As String
    Dim iTemp               As Integer

    Set objIADs = GetObject("LDAP://RootDSE")

    'such as DC=mydomain,DC=example,DC=com
    strDomainDN = objIADs.Get("defaultNamingContext")
    'such as mydnsname.example.com
    strDomainDNSName = iADSSystem.DomainDNSName

     strURL = "LDAP://" & strDomainDNSName & "/"
     If strContainer <> "" Then
         ' If strContainer is not empty, reference the object as a container.
         strContainerDN = "CN=" + strContainer + "," + strDomainDN
     Else
         ' If strContainer is empty, reference the user under the domain.
         strContainerDN = strDomainDN
     End If
     ' Check whether to return a URL to the user, or to the container of the user.
     If Not IsContainerURL Then
         ' Check whether a user name was supplied.
         If strUserName = "" Then
            ' If no user name was supplied, generate a random user name.
            ' For example, "CDOUser_142302"
            Randomize
            iTemp = Int(Rnd * 33554432)
            strUserName = "CDOUser" & "_" & Str(iTemp)
         End If
         ' Create the URL that will be returned, containing the user name.
         strURL = strURL & "CN=" & strUserName & "," & strContainerDN
     Else
         ' If the function is only returning a URL to the container, create that URL.
         strURL = strURL & strContainerDN
     End If
    ' Clean up.
    Set objIADs = Nothing
    Set iADSSystem = Nothing
End Sub

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.