Using Directory Services saves some settings and not others


G

Guest

#Region "Imports

Imports ADSSECURITYLi
Imports System.Diagnostic
Imports Scriptin
Imports ActiveD
Imports System.DirectoryService
Imports System.Configuration.ConfigurationSetting
Imports System.I

#End Regio

Public Class Utilit
Inherits System.Web.UI.Pag

#Region "Constants

Const ADS_UF_SCRIPT = &H
Const ADS_UF_ACCOUNTDISABLE = &H
Const ADS_UF_HOMEDIR_REQUIRED = &H
Const ADS_UF_LOCKOUT = &H1
Const ADS_UF_PASSWD_NOTREQD = &H2
Const ADS_UF_PASSWD_CANT_CHANGE = &H4
Const ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = &H8
Const ADS_UF_TEMP_DUPLICATE_ACCOUNT = &H10
Const ADS_UF_NORMAL_ACCOUNT = &H20
Const ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = &H80
Const ADS_UF_WORKSTATION_TRUST_ACCOUNT = &H100
Const ADS_UF_SERVER_TRUST_ACCOUNT = &H200
Const ADS_UF_DONT_EXPIRE_PASSWD = &H1000
Const ADS_UF_MNS_LOGON_ACCOUNT = &H2000
Const ADS_UF_SMARTCARD_REQUIRED = &H4000
Const ADS_UF_TRUSTED_FOR_DELEGATION = &H8000
Const ADS_UF_NOT_DELEGATED = &H10000
Const ADS_UF_USE_DES_KEY_ONLY = &H20000
Const ADS_UF_DONT_REQUIRE_PREAUTH = &H40000
Const ADS_UF_PASSWORD_EXPIRED = &H80000
Const ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = &H100000

#End Regio

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa

Dim AccountName as strin
Dim Password as Strin

AccountName = "SEA1010
Password = "123456789

AddAccount(AccountName, Password

End Su

Private Sub SetPassword(ByVal AccountName As String, ByVal Password As String

Dim MyDirectoryEntry As DirectoryEntr
Dim MyDirectorySearcher As DirectorySearche
Dim MyGroup As DirectoryEntr
Dim MyUser As DirectoryEntr
Dim MyUserAccountControl As Intege

MyDirectoryEntry = New DirectoryEntry("LDAP://" & AppSettings("LDAPPath"), AppSettings("Domain") & "\" & AppSettings("Administrator"), AppSettings("Password")
MyDirectorySearcher = New DirectorySearcher(MyDirectoryEntry
MyDirectorySearcher.Filter = "(samAccountName=" & AccountName & ")
MyUser = New DirectoryEntry(MyDirectorySearcher.FindOne.GetDirectoryEntry.Path
MyUser.AuthenticationType = AuthenticationTypes.Secur
MyUser.Invoke("SetPassword", New Object() {Password}

MyUserAccountControl = MyUser.Properties("userAccountControl").Valu
MyUser.Properties("userAccountControl").Value = MyUserAccountControl Or ADS_UF_DONT_EXPIRE_PASSW
MyUser.Properties("userAccountControl").Value = MyUserAccountControl Or ADS_UF_PASSWD_CANT_CHANG

MyDirectoryEntry.CommitChanges(
MyDirectoryEntry.RefreshCache(

End Su

Private Sub CreateAccount(ByVal AccountName As String

Dim MyDirectoryEntry As DirectoryEntr
Dim MyDirectorySearcher As DirectorySearche
Dim MyGroup As DirectoryEntr
Dim MyUser As DirectoryEntr

MyDirectoryEntry = New DirectoryEntry("LDAP://" & AppSettings("LDAPPath"), AppSettings("Domain") & "\" & AppSettings("Administrator"), AppSettings("Password")
MyUser = MyDirectoryEntry.Children.Add("cn=" & AccountName & ",ou=" & AppSettings("LDAPOU"), "user"
MyUser.Properties("sn").Add(AccountName
MyUser.Properties("displayName").Add(AccountName
MyUser.Properties("samAccountName").Add(AccountName
MyUser.Properties("homeDirectory").Add(AppSettings("FTPDirectoryPath") & AccountName
MyUser.Properties("accountExpires").Add(0

MyUser.CommitChanges(
MyUser.RefreshCache(

End Su

Private Sub SetGroup(ByVal AccountName As String

Dim MyDirectoryEntry As DirectoryEntr
Dim MyDirectorySearcher As DirectorySearche
Dim MyGroup As DirectoryEntr
Dim MyUser As DirectoryEntr

MyDirectoryEntry = New DirectoryEntry("LDAP://" & AppSettings("LDAPPath"), AppSettings("Domain") & "\" & AppSettings("Administrator"), AppSettings("Password")
MyDirectorySearcher = New DirectorySearcher(MyDirectoryEntry
MyDirectorySearcher.Filter = "(samAccountName=" & AccountName & ")"
MyUser = New DirectoryEntry(MyDirectorySearcher.FindOne.GetDirectoryEntry.Path)

MyDirectoryEntry = New DirectoryEntry("LDAP://" & AppSettings("LDAPPath") & "/CN=" & AppSettings("LDAPCN") & ",OU=" & AppSettings("LDAPOU") & " , " & AppSettings("LDAPDCPath"), AppSettings("Domain") & "\" & AppSettings("Administrator"), AppSettings("Password"))
MyDirectoryEntry.Invoke("Add", New Object() {MyUser.Path.ToString()})

MyDirectoryEntry.CommitChanges()
MyUser.RefreshCache()

End Sub

Private Sub EnableAccount(ByVal AccountName As String)

Dim MyDirectoryEntry As DirectoryEntry
Dim MyDirectorySearcher As DirectorySearcher
Dim MyGroup As DirectoryEntry
Dim MyUser As DirectoryEntry
Dim MyUserAccountControl As Integer

MyDirectoryEntry = New DirectoryEntry("LDAP://" & AppSettings("LDAPPath"), AppSettings("Domain") & "\" & AppSettings("Administrator"), AppSettings("Password"))
MyDirectorySearcher = New DirectorySearcher(MyDirectoryEntry)
MyDirectorySearcher.Filter = "(samAccountName=" & AccountName & ")"
MyUser = New DirectoryEntry(MyDirectorySearcher.FindOne.GetDirectoryEntry.Path)

MyUserAccountControl = MyUser.Properties("userAccountControl").Value

MyUser.Properties("userAccountControl").Value = MyUserAccountControl And Not ADS_UF_ACCOUNTDISABLE

MyDirectoryEntry.CommitChanges()
MyDirectoryEntry.RefreshCache()

End Sub

Private Sub DisableAccount(ByVal AccountName As String)

Dim MyDirectoryEntry As DirectoryEntry
Dim MyDirectorySearcher As DirectorySearcher
Dim MyGroup As DirectoryEntry
Dim MyUser As DirectoryEntry
Dim MyUserAccountControl As Integer

MyDirectoryEntry = New DirectoryEntry("LDAP://" & AppSettings("LDAPPath"), AppSettings("Domain") & "\" & AppSettings("Administrator"), AppSettings("Password"))
MyDirectorySearcher = New DirectorySearcher(MyDirectoryEntry)
MyDirectorySearcher.Filter = "(samAccountName=" & AccountName & ")"
MyUser = New DirectoryEntry(MyDirectorySearcher.FindOne.GetDirectoryEntry.Path)

MyUserAccountControl = MyUser.Properties("userAccountControl").Value
MyUser.Properties("userAccountControl").Add(MyUserAccountControl Or ADS_UF_ACCOUNTDISABLE)

MyDirectoryEntry.CommitChanges()
MyDirectoryEntry.RefreshCache()

End Sub

Private Sub AddAccount(ByVal AccountName, ByVal Password)

CreateAccount(AccountName)
SetPassword(AccountName, Password)
EnableAccount(AccountName)
SetGroup(AccountName)

End Sub

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

End Class
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top