C# .NET - Creating a windows user account

Asked By Dave Becker
18-Dec-06 11:26 AM

I have an app which needs to create a windows user account (I won't bore you with specifics) and I am using the code shown below. The code works fine when logged in locally but not when accessing via public address. The server is 2003 Standard it is not part of a domain nor a domain controller. Any ideas???

try
    {

      // local vars to imitate parameters
      string s_Username = "TestUser";
      string s_Password = "password";
      string s_Description = "Test User Account";

      DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
      // create a new user
      DirectoryEntry NewUser = AD.Children.Add(s_Username, "user");
      // set the properties
      NewUser.Invoke("SetPassword", new object[] { s_Password });
      NewUser.Invoke("Put", new object[] { "Description", s_Description });
      // commit the changes
      NewUser.CommitChanges();
      lblResult.Text = "Success!!";
    }
    catch (Exception err)
    {
      // set the error message
      lblResult.Text = err.Message;
    }

Create a Local Windows User Account [C#/.NET]  Create a Local Windows User Account [C#/.NET]

18-Dec-06 11:38 AM

Using the Windows net command, it’s easy to create local Windows User Accounts. The syntax for the net command is:

net user [username] [password] /ADD

The following C# function takes in three parameters -- username, password and home directory.

using System.Diagnostics;

public void CreateLocalUser(string username, string password, string homedir)
  {
  if (!Directory.Exists(homedir))
    Directory.CreateDirectory(homedir);
  
  Process MyProc = new Process();
  MyProc.StartInfo.WorkingDirectory = "C:\WINNT\SYSTEM32";
  MyProc.StartInfo.FileName = "net.exe";
  MyProc.StartInfo.UseShellExecute = false;
  MyProc.StartInfo.RedirectStandardError = true;
  MyProc.StartInfo.RedirectStandardInput = true;
  MyProc.StartInfo.RedirectStandardOutput = true;
  MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  
  MyProc.StartInfo.Arguments = @" user " + username + @" " + password + @" /ADD /ACTIVE:YES " +
    @"/EXPIRES:NEVER /FULLNAME:" + username + @" /HOMEDIR:""" + 
    homedir + @""" /PASSWORDCHG:NO /PASSWORDREQ:YES";
  
  MyProc.Start();
  MyProc.WaitForExit();
  MyProc.Close();
  }

It assumes a few settings for the user and creates a local User account. You can alter the settings to anything you want. Try “net help user” in your DOS prompt for what each of the switches mean/do.

reference

http://www.thejackol.com/2004/08/03/create-a-local-windows-user-account-cnet/

It's all about credentials and permissions.  It's all about credentials and permissions.

18-Dec-06 12:21 PM

If you are logged on locally as an Administrator (most people are, even though there are some very good reasons to run as least privilege), then you probably have the credentials and permission set to add a new Windows User.

However when you run your app from the public Internet, IIS will run it either as <IUser_MachineName> or the ASPNET account, both of which are weak accounts by design.  So you either need to give them permissions / roles allowing adding of users, or run your web app impersonating a different account that does.

 

Create New Account
help
Unable to install IIS using Pkgmgr in 64 bit machine Hi, I am creating a SetUP, in that, if IIS is not installed in the machine Iam trying to install it using Xml file using Code: In Command prompt : Start / W / D "path of the xml file" pkgmgr.exe / n:IIS.xml You can find the Xml file and log here: LogFile&XML.zip Please help an Unattend.xml file From the command window, run notepad.exe. To install only the IIS 7.0 default features, copy the following unattend.xml text into notepad. <?xml version = "1 version = "6.0.5308.6" language = "neutral" processorArchitecture = "x86" publicKeyToken = "31bf3856ad364e35" versionScope = "nonSxS" / > <selection name = "IIS-WebServerRole" state = "true" / > <selection name = "WAS-WindowsActivationService" state = "true" / > <selection name = "WAS-ProcessModel" state = "true selection name = "WAS-ConfigurationAPI" state = "true" / > < / package> < / servicing> < / unattend> If you want to install all IIS 7.0 features for evaluation purposes, use one of the following unattend.xml files. For version = "6.0.5308.6" language = "neutral" processorArchitecture = "x86" publicKeyToken = "31bf3856ad364e35" versionScope = "nonSxS" / > <selection name = "IIS-WebServerRole" state = "true" / > <selection name = "IIS-WebServer" state = "true" / > <selection name = "IIS-CommonHttpFeatures" state = "true" / > <selection name = "IIS-StaticContent" state = "true
How to install iis in windows 7 home basic hi frds how can install iis in windows 7 home basic HI To Install IIS 7.5 on Windows 7 You can perform this procedure using the user interface (UI the Windows Features dialog box, click Internet Information Services and then click OK . To Install IIS 7.5 on Windows 7 You can perform this procedure using the user interface (UI click OK . follow this link- http: / / technet.microsoft.com / en-us / library / cc731911.aspx Install IIS 7 1. To open the Windows Features dialog box, click Start , and then click Control displayed. Figure 5: Windows Security dialog box 5. Expand Internet Information Services . Additional categories of IIS features are displayed. Select Internet Information Services to choose the default features for installation. Figure 6: Windows Features dialog box - IIS 6. Expand the additional categories displayed, and select any additional features you want to install such as Web Management Tools. Figure 7: Windows Features dialog box - IIS feature selection 7. If you are installing IIS 7 for evaluation purposes, you may want
test my asp.net website hiii, i wanted to test my website over IIS. . . . for that. . i hav installed IIS. . . . .then i created virtual directory. . . . . . and undertook all the step reqiured but when i m yaa. . . . Default.aspx. . . . .of my website is listed under virtual directory. . . . . . . . . . . If you are using IIS 6, be aware that IIS disables dynamic content for security reasons. To permit IIS to serve dynamic content, the administrator must unlock required content in the Web service extensions node in IIS Manager. Check these links for more info - HTTP Error 404 - File or Directory not found" error message when you request dynamic content with IIS 6.0 - http: / / support.microsoft.com / kb / 315122 How system administrators can troubleshoot an "HTTP 404 - File not found" error message on a server that is running IIS - http: / / support.microsoft.com / kb / 248033 hi, check the correct version of framework in the
Error installing IIS on Win XP Pro SP 2 - HELP!! I'm using Add / Remove windows components to install IIS on my Win XP SP 1 system that was later upgraded to SP2 and has Windows Firewall and disabled VirusScan. Please help. I've spent days on trying to get IIS to work and i'm way behind schedule on getting a project done as a and no one has a solution rather than reinstall the XP SP1 and then install IIS. I've spent too much time in configuring this sytem that I can't do 45] returned from France fix with locale 409 [3 / 5 / 2005 12:57:45] OC_PREINITIALIZE:[iis] End. Return = 1 (OCFLAG_UNICODE) [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:[iis, (null)] Start. [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:3 / 31 / 2003 12:00:00 0.2600.2180: 6.0.2600.2180 (xpsp_sp2_rtm.040803-2158): x86: C: \ WINDOWS \ system32 \ Setup \ iis.dll [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:Set UnAttendFlag:OFF (File = '') [3 / 5 / 2005 5 / 2005 12:57:59] OC_CLEANUP:GlobalFastLoad = 1. [3 / 5 / 2005 12:57:59] OC_CLEANUP:[iis, (null)] Start. 0x0, 0x0 [3 / 5 / 2005 12:57:59] OC_CLEANUP: - -- Display status of services