|
You might have better luck using the System.DirectoryServices.AccountManagement namespace[^]. It provides a much cleaner interface than the raw DirectoryServices version.
Using context As New PrincipalContext(ContextType.Domain)
Using user As New UserPrincipal(context, UserName, Password, True)
user.DisplayName = DisplayName
user.GivenName = FirstName
user.MiddleName = MiddleInitial
user.Surname = LastName
user.EmailAddress = Email
user.Description = Description
user.HomeDrive = HomeDrive
user.HomeDirectory = HomeDirectory
user.ScriptPath = ScriptPath
If UserPrincipalName <> "" Then
user.UserPrincipalName = UserPrincipalName
End If
user.PasswordNeverExpires = True
user.UserCannotChangePassword = True
Dim myDirectoryEntry As DirectoryEntry = TryCast(user.GetUnderlyingObject(), DirectoryEntry)
If myDirectoryEntry IsNot Nothing Then
Utility.SetProperty(myDirectoryEntry, "PostalAddress", PostalAddress)
Utility.SetProperty(myDirectoryEntry, "StreetAddress", MailingAddress)
Utility.SetProperty(myDirectoryEntry, "HomePostalAddress", ResidentialAddress)
Utility.SetProperty(myDirectoryEntry, "Title", Title)
Utility.SetProperty(myDirectoryEntry, "HomePhone", HomePhone)
Utility.SetProperty(myDirectoryEntry, "TelephoneNumber", OfficePhone)
Utility.SetProperty(myDirectoryEntry, "Mobile", Mobile)
Utility.SetProperty(myDirectoryEntry, "FacsimileTelephoneNumber", Fax)
Utility.SetProperty(myDirectoryEntry, "Url", Url)
End If
user.Save()
Using group As GroupPrincipal = GroupPrincipal.FindByIdentity(context, IdentityType.Name, MemberOf)
If group IsNot Nothing Then
group.Members.Add(user)
group.Save()
End If
End Using
End Using
End Using
Using System.DirectoryServices.AccountManagement[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 9-Sep-16 9:03am.
|
|
|
|
|
Thank you Richard, I am going to try userPricipal...will report on success.
|
|
|
|
|
Hi Richard,
I have worked this code into my app and resolved bugs to get it to run however, I keep getting 'User name or password is incorrect' at
Using user As New UserPrincipal(oPrincipalContext, UserName, Password, True)
Is this the current user being created? I am not sure...what I was able to find was indications that this is indeed user.username and user.password. If so why do i still fail.
Hope you can assist.
Thanks again
|
|
|
|
|
The UserName and Password should be the username and password for the user you're creating.
Does your AD domain have password complexity requirements that you're failing to meet?
Is your code running as a user that has permission to create users in your domain?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
With my first piece of code that I submitted I did create everything about the user except for the ability to also create the Homepath dir/drive, account options pass exp/cannot change and setting the user's group.
I was hoping this would still be valid with the already successes and enhanced my whole process.
this is the code from you with slight changes:
Using user As New UserPrincipal(oPrincipalContext, UserName, Password, True)
user.DisplayName = DisplayName
user.GivenName = FirstName
user.MiddleName = MiddleInitial
user.Surname = LastName
user.EmailAddress = Email
user.Description = Description
user.HomeDrive = HomeDrive
user.HomeDirectory = HomeDirectory
user.ScriptPath = ScriptPath
If UserPrincipalName <> "" Then
user.UserPrincipalName = UserPrincipalName
Else
user.UserPrincipalName = UserName
End If
user.PasswordNeverExpires = True
user.UserCannotChangePassword = True
user.Enabled = True
Dim myDirectoryEntry As DirectoryEntry = TryCast(user.GetUnderlyingObject(), DirectoryEntry)
If myDirectoryEntry IsNot Nothing Then
Utility.SetProperty(myDirectoryEntry, "PostalAddress", PostalAddress)
Utility.SetProperty(myDirectoryEntry, "StreetAddress", MailingAddress)
Utility.SetProperty(myDirectoryEntry, "HomePostalAddress", ResidentialAddress)
Utility.SetProperty(myDirectoryEntry, "Title", Title)
Utility.SetProperty(myDirectoryEntry, "HomePhone", HomePhone)
Utility.SetProperty(myDirectoryEntry, "TelephoneNumber", OfficePhone)
Utility.SetProperty(myDirectoryEntry, "Mobile", Mobile)
Utility.SetProperty(myDirectoryEntry, "FacsimileTelephoneNumber", Fax)
Utility.SetProperty(myDirectoryEntry, "Url", Url)
End If
user.Save()
Using group As GroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, IdentityType.Name, MemberOF)
If group IsNot Nothing Then
group.Members.Add(user)
group.Save()
End If
End Using
Thanks for the fast repsonse
|
|
|
|
|
If you're getting a "username or password is incorrect" error, that means either:
- Your code is not running under an account which has permission to change Active Directory;
- The password you're trying to set is rejected by your password complexity rules;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Have just tested my old code and with the same account admin and pass i created the AD member with same username and password with the same 3 not created issues...then I deleted the AD member and attempted again with the code provided...no go...
I am really dumb struck...
|
|
|
|
|
OK, try passing Nothing as the password parameter to the UserPrincipal constructor. Then, after calling user.Save() , call user.SetPassword(Password) , followed by another user.Save() call.
Using user As New UserPrincipal(context, UserName, Nothing, True)
...
user.Save()
user.SetPassword(Password)
user.Save()
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I tried it and received a new error at the same place in the code
The supplied arguments cannot be null.
Thanks for persisting with me...
|
|
|
|
|
That's annoying.
Try the constructor that doesn't take the username and password as arguments:
Using user As New UserPrincipal(context)
user.SamAccountName = UserName
user.Name = UserName
user.Enabled = True
...
user.Save()
user.SetPassword(Password)
user.Save()
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi again Richard,
I will definitely try this on Monday and report. Thanks again and enjoy your weekend
|
|
|
|
|
Start of a new week, Hi Richard,
I have tried the change as you suggested but keep getting a "The user name or password is incorrect"
Truly hope you might have another suggestion...
|
|
|
|
|
Hi Richard,
still receives error "The user name or Password is incorrect"
hope you have another idea still
thanks
|
|
|
|
|
No. The code I posted seems to work for everyone else, so I can only assume there's something odd with your setup.
All I can suggest is that you try using your previous code to create the user, and use the new code to update the properties that you couldn't set:
Using context As New PrincipalContext(ContextType.Domain)
Using user As UserPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.Name, UserName)
If user IsNot Nothing Then
user.PasswordNeverExpires = True
user.UserCannotChangePassword = True
user.HomeDrive = HomeDrive
user.HomeDirectory = HomeDirectory
user.ScriptPath = ScriptPath
user.Save()
Using group As GroupPrincipal = GroupPrincipal.FindByIdentity(context, IdentityType.Name, MemberOf)
If group IsNot Nothing Then
group.Members.Add(user)
group.Save()
End If
End Using
End If
End Using
End Using
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This is excellent, thank you so much...it all works 100%.
One question only, please: When the 'user logon name' is inserted it has a drop down box with the domain listed. When I insert the username the drop-down is not selected with the domain...is there a way I can set this value.
Thanks again
|
|
|
|
|
According to this StackOverflow answer[^], you need to include the domain in the UserPrincipalName property:
Utility.SetProperty(myDirectoryEntry, "UserPrincipalName", "some.user@your.domain.local")
user.UserPrincipalName = "some.user@your.domain.local"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard,
Truly thank you for enduring with me on this issue. I have resolved all shortcomings I had and everything is inserted 100%.
Big thumbs up.
|
|
|
|
|
i want to make security system for university need help??
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
You need to specify a whole load more than that - and tell us what you have done so far as well.
Then tell us what help you need.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
How do you know you need help? It doesn't appear as if you've even started.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
guys,
I need to design a web page with file upload option in asp net mvc.
my requirement is, have few text boxes, dropdown list and need to upload file to support that data. these things to be save in db, and have generate that page dynamically.
Thanks
|
|
|
|
|
This is just a partial list of requirements. It is not a question or a description of a problem. Perhaps you can reword your posting to include a question, a description of a problem and make it sound less like you expect us to write your code for you (because we won't). And, in the future, posting in the appropriate forum might be better. There are web programming forums that might be a better fit for whatever question you actually have.
|
|
|
|
|
im beginner to mvc. im just asking basic idea, like how.
|
|
|
|
|
|
Dani_el wrote: im beginner to mvc. im just asking basic idea, like how.
The basic idea is to read tutorials, read articles, read books, take some classes at your local college or university. Reading how to get an answer to your question[^] might not be a bad idea either.
|
|
|
|
|