473,416 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Problem with Creating User Profiles on Windows XP

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

Jul 21 '05 #1
0 1788

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rajesh Kumar Mallah | last post by:
Hi, I face the following problem in transferring data from pgsql 7.3.4 to pgsql 7.4 RC1 . I am piping the output of pg_dumpall from 7.3 to 7.4 running on different port. The problem is there...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
0
by: Wayne Gibson | last post by:
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...
2
by: Paul Lemke | last post by:
Hello, I currently have an application that uses authenticates users via NTLM. I would like to provide a user a directory listing of a folder. Everyone has read access to the folder, but the only...
0
by: Rafa | last post by:
Hello, First, sorry because I am spanish and my english are not good. My problem is that i am trying to work with profiles in visual studio 2005, but i have an error. the error is that: ...
9
by: =?Utf-8?B?Sm9obiBBdXN0aW4=?= | last post by:
I have an app that prints entry tickets. If the printer driver is not set up exactly to detect the black mark on the back of the ticket, the tickets do not print correctly. Because of this, all...
5
by: =?Utf-8?B?cnZhbmdlbGRyb3A=?= | last post by:
Hello, I have a problem with our OnlineBackupService.exe. This is a Windows Service which is built in .Net 1.1 and basically grabs files from the file system and will try to upload them using...
6
by: goraya | last post by:
This is design level discussion about web applications. How I design application that support 1 million concurrent requests??
1
by: =?Utf-8?B?VGVycnk=?= | last post by:
I am using the My.Settings object to save user settings. During testing, I want to 'zero' out what has been saved to start with the defaults again. Where doies the framwork actually persist these...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.