|
This is a test of the CP Forum posting system. If this were an actual posting, it would include useful content and might provide some context for a question.
Seriously though, I'm not going to click your link. Please explain what you need assistance with in as much detail as you can provide.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
|
I want to avoid referencing anything related to VB if at all possible.
Is there a C# equivalent to Microsoft.VisualBasic.FileIO.TextFieldParser floating around anywhere?
EDIT ======================
As a quick fix, I came up with the following. I don't know what all the VB version does, but this code appears to correctly parse quoted strings...
public partial class TextFieldParser
{
public TextFieldParser()
{
}
public string[] ParseFields(string text)
{
string[] parts = text.Split(',');
List<string> newParts = new List<string>();
bool inQuotes = false;
string currentPart = string.Empty;
for (int i = 0; i < parts.Length; i++)
{
string part = parts[i];
inQuotes = (inQuotes || part.StartsWith("\""));
if (inQuotes)
{
currentPart = (string.IsNullOrEmpty(currentPart))? part : string.Format("{0},{1}", currentPart, part);
}
else
{
currentPart = part;
}
inQuotes = (inQuotes && !part.EndsWith("\""));
if (!inQuotes)
{
currentPart = currentPart.Replace("\"", "");
newParts.Add(currentPart);
currentPart = string.Empty;
}
}
return newParts.ToArray();
}
}
".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
modified 9-Sep-16 12:27pm.
|
|
|
|
|
For CSV / TDF files, I tend to use this library[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Actually, I'm writing a (evidently much lighter) CSV parser, and was gonna write an article, but now I don't know.
".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
|
|
|
|
|
|
Hi, I want to know in simple words,What is Assembly in Dotnet framework???
|
|
|
|
|
Plenty of definitions here[^].
This space for rent
|
|
|
|
|
Hi Guys, hope my question is formed well enough and thanks for everyone who contributes to this site as I have had many successes in projects through Codeproject. However it is my first time asking a question. I have been struggling now for a good couple of days with the above. I am reading to AD and writing but for the life of me i just seem not to be able to write to the above. Following is my code: If any one can please assist me or point me into the right direction...please...and thank you really a lot.
Dim RootDSE As String
Dim DSESearcher As DirectorySearcher = New System.DirectoryServices.DirectorySearcher
Try
RootDSE = DSESearcher.SearchRoot.Path
RootDSE = RootDSE.Insert(7, Utility.ADUsersPath & ",")
Dim myDE As DirectoryEntry = New DirectoryEntry(RootDSE)
Dim myEntries As DirectoryEntries = myDE.Children
Dim myDirectoryEntry As DirectoryEntry = myEntries.Add("CN=" + DisplayName, "user")
Utility.SetProperty(myDirectoryEntry, "givenName", FirstName)
Utility.SetProperty(myDirectoryEntry, "initials", MiddleInitial)
Utility.SetProperty(myDirectoryEntry, "sn", LastName)
Utility.SetProperty(myDirectoryEntry, "displayName", DisplayName)
If UserPrincipalName <> "" Then
Utility.SetProperty(myDirectoryEntry, "UserPrincipalName", UserPrincipalName)
Else
Utility.SetProperty(myDirectoryEntry, "UserPrincipalName", UserName)
End If
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, "mail", Email)
Utility.SetProperty(myDirectoryEntry, "Url", Url)
Utility.SetProperty(myDirectoryEntry, "sAMAccountName", UserName)
Utility.SetProperty(myDirectoryEntry, "UserPassword", Password)
Utility.SetProperty(myDirectoryEntry, "description", Description)
'Utility.SetProperty(myDirectoryEntry, "homeDrive", HomeDrive)
'Utility.SetProperty(myDirectoryEntry, "homeDirecrtory", HomeDirectory)
Utility.SetProperty(myDirectoryEntry, "scriptPath", ScriptPath)
myDirectoryEntry.Properties("userAccountControl").Value = Utility.UserStatus.Enable
''1ST COMMIT VALIDATES AGAINST USERNAME AND DISPLAY NAME RESPECTIVELY
myDirectoryEntry.CommitChanges()
SetMember(DisplayName, MemberOF)
''USE DISPLAY: USERNAME FAIL WHEN CREATING PASSWORD
myDirectoryEntry = GetUser(DisplayName)
Utility.SetUserPassword(myDirectoryEntry, Password)
myDirectoryEntry.Properties("userAccountControl").Value = Utility.ADAccountOptions.UF_DONT_EXPIRE_PASSWD
myDirectoryEntry.CommitChanges()
myDirectoryEntry.Properties("userAccountControl").Value = Utility.ADAccountOptions.UF_PASSWD_CANT_CHANGE
myDirectoryEntry.CommitChanges()
Return myDirectoryEntry
Catch ex As Exception
If Not ex.InnerException Is Nothing Then
MsgBox(ex.InnerException.Message)
End If
'Throw New Exception(ex.Message)
MsgBox(ex.Message)
Throw ex
End Try
End Function
Private Function SetMember(ByVal DisplayName As String, ByVal memberOF As String) As Integer
Dim RootDSE As String
Dim DSESearcher As DirectorySearcher = New System.DirectoryServices.DirectorySearcher
RootDSE = DSESearcher.SearchRoot.Path
RootDSE = RootDSE.Insert(7, Utility.ADUsersPath & ",")
Dim myDE As DirectoryEntry = New DirectoryEntry(RootDSE)
Dim myEntries As DirectoryEntries = myDE.Children
Try
'For Each thisGroup As [String] In memberOF
Dim myParent As DirectoryEntries = myDE.Parent.Children
Dim newGroup As DirectoryEntry = myParent.Find("CN=" + memberOF, "group")
If newGroup IsNot Nothing Then
Dim myDirectoryEntry As DirectoryEntry = myEntries.Add("CN=" + DisplayName, "user")
newGroup.Invoke("Add", New Object() {myDirectoryEntry.Path.ToString()})
myDirectoryEntry.CommitChanges()
newGroup.Close()
End If
'Next
Catch ex As Exception
If Not ex.InnerException Is Nothing Then
MsgBox(ex.InnerException.Message)
End If
'Throw New Exception(ex.Message)
MsgBox(ex.Message)
End Try
End Function
Again, thank you for any input
|
|
|
|
|
You kind of forgot to describe the problem, where it happens in your code and any error messages.
|
|
|
|
|
Hi Dave,
Thank you for the reply.
After a lot of searching i sort of have resolved errors. However my problem is that I cannot find the correct way to update the (user account settings in AD i.e. password cannot change, do not expire etc.) as well as how to (update the homefolder drive and directory) and set (the member of group)...with the memberof group i get an error that says 'Object does not exist' so I am guessing the group Staff does not exist or spelling might be different...will confirm this still
'Utility.SetProperty(myDirectoryEntry, "homeDrive", HomeDrive)
'Utility.SetProperty(myDirectoryEntry, "homeDirecrtory", HomeDirectory)
myDirectoryEntry.CommitChanges()
SetMember(DisplayName, MemberOF)
''USE DISPLAY: USERNAME FAIL WHEN CREATING PASSWORD
myDirectoryEntry = GetUser(DisplayName)
Utility.SetUserPassword(myDirectoryEntry, Password)
myDirectoryEntry.Properties("userAccountControl").Value = Utility.ADAccountOptions.UF_DONT_EXPIRE_PASSWD
myDirectoryEntry.CommitChanges()
myDirectoryEntry.Properties("userAccountControl").Value = Utility.ADAccountOptions.UF_PASSWD_CANT_CHANGE
myDirectoryEntry.CommitChanges()
I was hoping that someone who were able to have done these updates could point me in the correct direction as to whether my syntax etc. is correct to update AD. This was a whole new experience and with a lot of searching and frustrations I got this far.
If at all possible a nudge or something more would be excellent.
Thanks again.
|
|
|
|
|
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
|
|
|
|
|