How do I find a user and the security group they belong to in active directory with their username and password in C#?
I'm trying to get a user out of Active directory using their username and password. Does anyone know how to do this in C# in addition to getting the security group they belong to?
Edit: This problem got more complicated 开发者_StackOverflow社区(the requirements changed on me after a meeting). The security groups are nested within AD.
Take a look here: Finding what Groups/Distribution lists a specific user belongs to in active directory. Main point is related to tokenGroups
property. BTW, you don't need to get with user password, just it's username.
Look into the DirectoryEntry class.
Here is a sample:
Dim dirEntry As DirectoryEntry
dirEntry = New DirectoryEntry("your LDAP info", "administrator", "password")
Dim entries As DirectoryEntries = dirEntry.Children
'' // Set login name and full name.
Dim newUser As DirectoryEntry = entries.Add("CN=JONNY BOY", "User")
newUser.Properties("sAMAccountName").Add("jboy")
newUser.CommitChanges()
newUser.Invoke("SetPassword", "hi2343145gfdtgwdt")
Dim flags As Integer
flags = CInt(newUser.Properties("userAccountControl").Value)
'' //enable user below
newUser.Properties("userAccountControl").Value = flags And Not &H2
'' //disable user below
newUser.Properties("userAccountControl").Value = flags Or &H1
'' //lockout property
Dim l As Long
l = CType(newUser.Properties("lockoutTime").Value, Long)
If l <> 0 Then
'' //account is locked out
'' //so how do we unlock it?
'' //we unlock it by setting it to 0
newUser.Properties("lockoutTime").Value = 0
Else
'' //account is 0 it is NOT locked out
End If
newUser.CommitChanges()
Dim j As DirectoryEntry = entries.Find("CN=JONNY BOY", "User")
j.Properties("mail").Value = "jon@yahoo.com"
j.CommitChanges()
精彩评论