Active Directoty

Hey everyone,
I'm working on a project to set up a new user in AD using VB. I
have the connection working and I'm able to get most of the settings
done. However I can't find the properties to set the password to
never expire and the user cannot change password. I'd also like to
be able to set the logon hours as well. If someone can help me out with
those properties in VB I would appreciate it. Thanks a lot!

[461 byte] By [RedParrot] at [2008-1-10]
# 1

Hi Red,

This code sample can set the password to Never Expire and User cannot change password.

Code Block

Imports System.DirectoryServices

Try

Dim AD As New DirectoryEntry("WinNT://" + Environment.MachineName + ",computer")

Dim NewUser As DirectoryEntry = AD.Children.Add("TestUser", "user")

NewUser.Invoke("SetPassword", New Object() {"!123abc#"})

'User cannot change password

Dim ADS_UF_PASSWD_CANT_CHANGE As Integer = &H40

'Password Never Expires

Dim ADS_UF_DONT_EXPIRE_PASSWD As Integer = &H10000

NewUser.Invoke("Put", New Object() {"userFlags", ADS_UF_DONT_EXPIRE_PASSWD Or ADS_UF_PASSWD_CANT_CHANGE})

NewUser.Invoke("Put", New Object() {"Description", "Test User from .NET"})

NewUser.CommitChanges()

Dim grp As DirectoryEntry

grp = AD.Children.Find("Guests", "group")

If grp IsNot Nothing Then

grp.Invoke("Add", New Object() {NewUser.Path.ToString()})

End If

MessageBox.Show("Account Created Successfully")

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

About the LoginHours setting, please check the 5.3 Section: LoginHours inside this .pdf document for code sample.

Tip: objUser.Properties("LoginHours").Value
objUser.Properties("LoginHours").Add(value)

Some good samples.

How to add a new user using DirectoryServices?

How to manage Windows local user accounts

This forum is more appropriate for AD issues.

ASP.NET Forums Data Access Active Directory and LDAP

Regards,

Martin

MartinXie-MSFT at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...