For troubleshooting, you might check this attribute to see if it did when
the problem occurs. There is also a value you can get from
IADsObjectOptions (available via the Invoke method) to get the current
server name from the DirectoryEntry without additional binding.
Joe K.
"Paul Williams [MVP]" <ptw...@hotmail.com> wrote in message
news:%23Qlx6lR...@TK2MSFTNGP09.phx.gbl...
>I don't know .NET, but from looking at your code I wonder if it would make
> sense to get the dNSHostName attribute from RootDSE (via serverless bind,
> like you are doing), and then use this in all subsequent binds. This way
> you are ensuring that you are using the same server that you created your
> user on when you instantiate the group.
>
> But I might be way off - there might be an issue with your code.
> Therefore
> I'm cross-posting this into the ADSI group.
>
> I've attached the original message onto this message, as I don't top or
> bottom post generally.
>
> --
> Paul Williams
> Microsoft MVP - Windows Server - Directory Services
> http://www.msresource.net | http://forums.msresource.net
>
>
> "ken wiley" <ken....@edmonton.ca> wrote in message
> news:1131057899.9...@o13g2000cwo.googlegroups.com...
> Here is my code:
>
> Public Function Add(ByVal FirstName As String, ByVal MiddleInitial As
> String, _
> ByVal LastName As String, _
> ByVal EmployeeNumber As Integer, ByVal
> PostalAddress As String, _
> ByVal MailingAddress As String, ByVal
> ResidentialAddress As String, _
> ByVal Title As String, ByVal HomePhone As String,
> _
> ByVal OfficePhone As String, ByVal Mobile As
> String, _
> ByVal Fax As String, ByVal Email As String, ByVal
> Url As String, _
> ByVal JobClassificationCode As String) As
> InsertUserResponse
>
> Dim NewUserName As String
> Dim NewUserPassword As String
> Dim NewDistinguishedName As String
> Dim UserPrincipalName As String
> Dim sAMAccountName As String
> Dim JCC_ADPath As String
> Dim TemplateUser As String
> Dim myResponse As New InsertUserResponse
>
> If Not UserExists(FirstName & " " & LastName) Then
> Select Case JobClassificationCode.ToLower
> Case "cst"
> JCC_ADPath = "LDAP://coevm.ads/OU=Self Managed
> Users,OU=Users,OU=coe,dc=coevm,dc=ads"
> TemplateUser = "zz Self Managed"
> Case "sa"
> JCC_ADPath = "LDAP://coevm.ads/OU=Self Managed
> Users,OU=Users,OU=coe,dc=coevm,dc=ads"
> TemplateUser = "zz Self Managed"
> Case "bi"
> JCC_ADPath = "LDAP://coevm.ads/OU=Managed
> Users,OU=Users,OU=coe,dc=coevm,dc=ads"
> TemplateUser = "zz Managed"
> Case Else
> JCC_ADPath = "LDAP://coevm.ads/OU=Managed
> Users,OU=Users,OU=coe,dc=coevm,dc=ads"
> TemplateUser = "zz Managed"
> End Select
>
> Dim RootDSE As String
> Dim DSESearcher As System.DirectoryServices.DirectorySearcher = New
> _
> System.DirectoryServices.DirectorySearcher(New
> System.DirectoryServices.DirectoryEntry(JCC_ADPath, ADUser, ADPassword,
> AuthenticationTypes.Secure))
> RootDSE = JCC_ADPath
> Dim myDE As System.DirectoryServices.DirectoryEntry = New
> DirectoryEntry(RootDSE, ADUser, ADPassword, AuthenticationTypes.Secure)
> Dim myEntries As DirectoryEntries = myDE.Children
> Dim myDirectoryEntry As System.DirectoryServices.DirectoryEntry =
> myEntries.Add("CN=" & FirstName & " " & LastName, "user")
> SetProperty(myDirectoryEntry, "givenName", FirstName)
> SetProperty(myDirectoryEntry, "initials", MiddleInitial)
> SetProperty(myDirectoryEntry, "sn", LastName)
> SetProperty(myDirectoryEntry, "UserPrincipalName",
> GetUserName(FirstName, LastName, JobClassificationCode) & "@coevm.ads")
> SetProperty(myDirectoryEntry, "EmployeeNumber", EmployeeNumber)
> SetProperty(myDirectoryEntry, "DisplayName", FirstName & " " &
> LastName)
> SetProperty(myDirectoryEntry, "PostalAddress", PostalAddress)
> SetProperty(myDirectoryEntry, "StreetAddress", MailingAddress)
> SetProperty(myDirectoryEntry, "HomePostalAddress",
> ResidentialAddress)
> SetProperty(myDirectoryEntry, "Title", Title)
> SetProperty(myDirectoryEntry, "HomePhone", HomePhone)
> SetProperty(myDirectoryEntry, "TelephoneNumber", OfficePhone)
> SetProperty(myDirectoryEntry, "Mobile", Mobile)
> SetProperty(myDirectoryEntry, "FacsimileTelephoneNumber", Fax)
> SetProperty(myDirectoryEntry, "mail", Email)
> SetProperty(myDirectoryEntry, "Url", Url)
> SetProperty(myDirectoryEntry, "sAMAccountName",
> GetUserName(FirstName, LastName, JobClassificationCode))
> SetProperty(myDirectoryEntry, "UserPassword",
> CreatePassword(GetUserName(FirstName, LastName,
> JobClassificationCode)))
> myDirectoryEntry.Properties("userAccountControl").Value =
> UserStatus.Enable
> Try
> myDirectoryEntry.CommitChanges()
> Catch ex As Exception
> Throw ex
> End Try
> myDirectoryEntry = GetUserDE(FirstName & " " & LastName, JCC_ADPath)
> SetUserPassword(myDirectoryEntry,
> CreatePassword(GetUserName(FirstName, LastName,
> JobClassificationCode)))
> SetProperty(myDirectoryEntry, "pwdLastSet", 0)
> Try
> myDirectoryEntry.CommitChanges()
> myDE.CommitChanges()
> myDirectoryEntry.Close()
> Catch ex As Exception
> Throw ex
> End Try
> AddToGroupsFromTemplateUser(FirstName & " " & LastName, JCC_ADPath,
> TemplateUser)
> myResponse.Insert_User_Response.AddInsert_User_ResponseRow(True,
> "User created", FirstName & " " & LastName,
> CreatePassword(GetUserName(FirstName, LastName,
> JobClassificationCode)))
> Else
> myResponse.Insert_User_Response.AddInsert_User_ResponseRow(True,
> "User exists", FirstName & " " & LastName, "")
> End If
> Return myResponse
> End Function
>
> Friend Sub SetUserPassword(ByVal oDE As DirectoryEntry, ByVal Password
> As String)
> oDE.Invoke("SetPassword", New Object() {Password})
> End Sub
>
> Friend Function GetGroupsFromTemplate(ByVal UserName As String) As
> StringCollection
> Dim myDE As DirectoryEntry
> Dim myGroups As New StringCollection
> Dim de As DirectoryEntry = GetDirectoryObject()
> Dim deSearch As DirectorySearcher = New DirectorySearcher
> Dim I As Integer
> deSearch.SearchRoot = de
> deSearch.Filter = "(&(objectClass=user)(CN=" + UserName + "))"
> deSearch.SearchScope = SearchScope.Subtree
> Dim results As SearchResult = deSearch.FindOne()
> If Not (results Is Nothing) Then
> myDE = results.GetDirectoryEntry()
> Dim iCount As Integer = myDE.Properties("MemberOf").Count
> If iCount > 0 Then
> 'Retrieve group membership from Windows ADs and add to arraylist
> For I = 0 To iCount - 1
> Dim gADs As String = myDE.Properties("MemberOf").Item(I)
> Dim myGroup As String = Left(gADs, (InStr(gADs, ",") - 1))
> myGroups.Add(myGroup.Replace("CN=", ""))
> Next
> End If
> End If
> Return myGroups
> End Function
>
> Friend Sub AddGroupsForUser(ByVal UserName As String, ByVal ADPath As
> String, ByVal GroupCollection As StringCollection)
> 'Get object in ADs
> For Each myGroup As String In GroupCollection
> Dim grp As DirectoryEntry = GetGroup(myGroup)
> Dim usr As DirectoryEntry = GetUserDE(UserName, ADPath)
> If Not grp Is Nothing Then
> 'set authentication info for using ADs
> usr.AuthenticationType = AuthenticationTypes.Secure
> usr.Username = ADUser
> usr.Password = ADPassword
> grp.AuthenticationType = AuthenticationTypes.Secure
> grp.Username = ADUser
> grp.Password = ADPassword
> ' If Not IsMember(usr, grp) Then
> Dim strDisName As String =
> usr.Properties("distinguishedName").Value
> grp.Properties("Member").Add(strDisName)
> grp.CommitChanges()
> ' End If
> End If
> Next
> End Sub
>
> Sub AddToGroupsFromTemplateUser(ByVal UserName, ByVal ADPath, ByVal
> TemplateName)
> Dim templateGroups As StringCollection =
> GetGroupsFromTemplate(TemplateName)
> AddGroupsForUser(UserName, ADPath, templateGroups)
> End Sub
>
>
>