Part 7 Create the Account
Create the class that will create the user account in Active Directory.
Once again, this example uses the AresAdmin account used in the ChangePassword.cs class.
Copy the method used to create the Globals.cs class but this time call it ‘CreateAccount.cs’.
Change the code inside CreateAccount.cs to the following:
using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Security;
using System.Security.AccessControl;
using System.Runtime.InteropServices;
using System.IO;
namespace Ares
{
class CreateAccount
{
public async void AccountCreation(string CardReaderValue)
{
string userName = null;
string passwordstring = null;
try
{
#region payload
using (var pc = new PrincipalContext(ContextType.Domain, “mydomain”, Globals.cnSite, “AresAdmin”, “123456789012345”))
{
using (var up = new UserPrincipal(pc))
{
up.SamAccountName = CardReaderValue;
up.UserPrincipalName = CardReaderValue + “@myPrincipalName.co.uk”;
up.GivenName = Globals.foreName;
up.Name = CardReaderValue;
up.Surname = Globals.surName;
up.DisplayName = Globals.foreName + ” ” + Globals.surName;
up.Enabled = true;
up.ExpirePasswordNow();
up.Save();
PrincipalContext subcontext = new PrincipalContext(ContextType.Domain, “mydomain.local”, Globals.cnSite, “AresAdmin”, “123456789012345”);
UserPrincipal user = UserPrincipal.FindByIdentity(subcontext, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, CardReaderValue);
if (user != null)
{
DirectoryEntry addDetails = (DirectoryEntry)user.GetUnderlyingObject();
addDetails.Properties[“ProfilePath”].Value = Globals.profileDir;
addDetails.Properties[“homedrive”].Value = “U:”;
addDetails.Properties[“homedirectory”].Value = Globals.homeDir;
addDetails.Properties[“Title”].Value = “Student”;
addDetails.Properties[“Department”].Value = Globals.adGroup;
addDetails.Properties[“Description”].Value = “New User Account”;
addDetails.Properties[“facsimileTelephoneNumber”].Value = Globals.miFare;
addDetails.Properties[“company”].Value = Globals.ExchPolicy;
addDetails.CommitChanges();
}
#region Create the Home Folder
if (!Directory.Exists(Globals.homeDir))
{
Directory.CreateDirectory(Globals.homeDir);
}
string varAccountName = Globals.accountname;
string varHomeDir = Globals.homeDir;
string varSiteVar = Globals.siteVar;
string varAdGroup = Globals.adGroup;
string varCnValue = Globals.cnValue;
string varExchangeDB = Globals.exchangeDB;
System.Threading.Thread.Sleep(10000);
try
{
FileSystemRights Rights;
//Set the permissions on the new folder
Rights = FileSystemRights.Modify;
bool modified;
InheritanceFlags none = new InheritanceFlags();
none = InheritanceFlags.None;
//set on dir itself
FileSystemAccessRule accessRule = new FileSystemAccessRule(varAccountName, Rights, none, PropagationFlags.NoPropagateInherit, AccessControlType.Allow);
DirectoryInfo dInfo = new DirectoryInfo(varHomeDir);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out modified);
//Always allow objects to inherit on a directory
InheritanceFlags iFlags = new InheritanceFlags();
iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
//Add Access rule for the inheritance
FileSystemAccessRule accessRule2 = new FileSystemAccessRule(varAccountName, Rights, iFlags, PropagationFlags.InheritOnly, AccessControlType.Allow);
dSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule2, out modified);
dInfo.SetAccessControl(dSecurity);
}
catch
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@”\\myServer\AresErrorReports\UserFolder” + varSiteVar + “.txt”, true))
{
file.WriteLine(varAccountName);
}
}
#endregion
#region Add Account to Site Specific Student Group
try
{
DirectoryEntry addMembership = new DirectoryEntry(“LDAP://mydomain.local/CN=” + varAdGroup + “,OU=Special Groups,DC=mydomain,DC=local”, userName, passwordstring);
addMembership.Properties[“member”].Add(varCnValue);
addMembership.CommitChanges();
}
catch
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@”\\myServer\AresErrorReports\” + varSiteVar + varExchangeDB + “.txt”, true))
{
file.WriteLine(varAccountName);
}
}
#endregion
varAccountName = “”;
varHomeDir = “”;
varSiteVar = “”;
varAdGroup = “”;
varCnValue = “”;
varExchangeDB = “”;
}
}
#endregion
}
catch (Exception e)
{
Globals.errorCode = e.ToString();
Globals.finalTesting = e.ToString();
}
Globals.errorCode = “Account Created : ” + Globals.miFare;
}
private static bool SetAdParameter(DirectoryEntry entry, string name, string value)
{
try
{
entry.InvokeSet(name, new object[] { value });
entry.CommitChanges();
return true;
}
catch (Exception)
{
return false;
}
}
private static T GetAdParameter<T>(DirectoryEntry entry, string name)
{
try
{
return (T)entry.InvokeGet(name);
}
catch (Exception)
{
return default(T);
}
}
private static string GetStringFromSecureString(SecureString secStr)
{
if (secStr == null)
{
return null;
}
IntPtr pPlainText = IntPtr.Zero;
try
{
pPlainText = Marshal.SecureStringToBSTR(secStr);
return Marshal.PtrToStringBSTR(pPlainText);
}
finally
{
if (pPlainText != IntPtr.Zero)
{
Marshal.FreeBSTR(pPlainText);
}
}
}
async System.Threading.Tasks.Task WaitMethodShort()
{
await System.Threading.Tasks.Task.Delay(10000);
}
}
}