Hi all,
Please ignore the other post.. The cat jumped on the machine and sent it
before I could stop it!!
Was wondering if anybody has expericence this problem..
I am writting an application in C# using windows forms, to create windows
user profiles and then updating the registry information for the new
profiles.
I have managed to create the user successfully and thought that I was
writting the registry information correctly.
That was until I checked the 'C:\Documents and Settings' Directory, when I
noticed that the new users directories did not reflect their names. But
instead comprised of non printable characters!!!
Here is the code sample in C#
//////////////////////////////////////////
DirectoryEntry NewUser;
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName
+ ",computer");
// delete user when existing
try
{
// this throws when no such user
NewUser = AD.Children.Find(strNodeName, "User");
MessageBox.Show("User already exists!!");
return;
}// Catch not found exception
catch(COMException cex)
{
Console.WriteLine(cex.Message);
}
// Add user using the user schema
NewUser = AD.Children.Add(strNodeName, "user");
NewUser.Properties["description"].Add(this.txtFirstName.Text + " " +
this.txtSurname.Text);
// NewUser.Properties["PasswordExpired"].Add(1); // user must change
password at next login
// Set user flags sample here sets Account disabled, pwd can't change
// this flag is different for LDAP accounts
NewUser.Properties["userFlags"].Add( UF_NORMAL_ACCOUNT |
UF_DONT_EXPIRE_PASSWD );
// invoke native method 'SetPassword' before commiting
// for AD domain accounts this must be done after commiting
NewUser.Invoke("SetPassword", new Object[] { this.txtPassword.Text });
NewUser.CommitChanges();
// Add user to guests alias
DirectoryEntry grp = AD.Children.Find("Guests", "group");
if(grp.Name != null)
{
grp.Invoke("Add", new Object[] {NewUser.Path.ToString()});
}
MessageBox.Show("Account Created Successfully");
AD.Close();
//////////////////////////////////////////
Then I proceed to update the users Registry information!!
At this point their is no Directory for the user in "C:\Documents and
Settings\" directory
//////////////////////////////////////////////////
NativeMethods._PROFILEINFO profile;
IntPtr hToken = IntPtr.Zero;
hToken = NativeMethods.CreateLogonToken(UserAccount, UserDomain,
UserPassword);
if(hToken != IntPtr.Zero)
{
profile = new NativeMethods._PROFILEINFO();
profile.dwSize = Marshal.SizeOf(profile);
profile.lpUserName = UserAccount;
// Directory Still Not Created at this Point!!
int ret = Win32.LoadUserProfile(hToken, ref profile); // This function
Invokes
if(ret != 0)
{
// Use WMI to get the SID of the user (this requires W2K3 or XP)
using (ManagementObject account = new
ManagementObject("Win32_UserAccount.Domain='" + UserDomain +"',Name='" +
UserAccount + "'"))
{
account.Get();
// Get key wich is the root for the users hive loaded in HKU
UserSID = account["SID"].ToString();
if(UserSID.Length != 0)
{ // Now update the registry information for this user!!
UpdateRegistryInformation();
}
}
ret = NativeMethods.UnloadUserProfile(hToken, profile.hProfile);
}
}
[DllImport("advapi32", CharSet=CharSet.Auto, SetLastError=true)]
static extern int LogonUser(string lpszUserName, string lpszDomain, string
lpszPassword, int dwLogonType, int dwLogonProvider, ref HANDLE hToken);
[DllImport("userenv", CharSet=CharSet.Auto)]
internal static extern int LoadUserProfile(HANDLE hToken,ref _PROFILEINFO
lpProfileInfo);
public static HANDLE CreateLogonToken(string name, string domain, string
passwd)
{
HANDLE ret = IntPtr.Zero;
HANDLE tok = IntPtr.Zero;
System.Management.ManagementObject mo = new
System.Management.ManagementObject(new ManagementPath());
mo.Scope.Options.EnablePrivileges = true;
int result = LogonUser(name, ".", passwd, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref tok);
if (result != 0) // If success
{
ret = tok;
}
else
{
string temp = "LogonUser Error: ";
temp += Marshal.GetLastWin32Error().ToString();
MessageBox.Show( temp );
}
return ret;
}
///////////////////////////////
So I stopped writting to the registry information and then physically logged
in as the user via the windows login screen.
This then created the directory correctly!!
Any ideas?
Thanks
Wayne