I created a web service which can be called from a form to add new users to the system. The final service will add users to Active Directory and Exchange in the required format (please note that FirstName
and LastName
are required fields in the form, so they will never be null
):
using System.Collections.Generic;
using System.Web.Services;
namespace NewStarterWebService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class NewStarter : WebService
{
[WebMethod]
public void CreateNewUser(
string firstName,
string middleName,
string lastName,
string jobTitle,
string department,
string office,
string role,
string manager)
{
var newUser = new NewUser(firstName, middleName, lastName, jobTitle, department, office, role, manager);
newUser.Create();
}
}
}
The NewUser
Class only has 1 method - Create()
and a constructor:
using System;
using System.Collections.Generic;
namespace NewStarterWebService
{
public class NewUser
{
public readonly string FullName;
public readonly string FirstName;
public readonly string MiddleName;
public readonly string LastName;
public readonly string Department;
public readonly string Office;
public readonly string JobTitle;
public readonly string Role;
public readonly string Manager;
public readonly string Email;
public readonly string DotName;
public readonly string HomeDirectory;
public readonly string Initials;
public readonly Dictionary<string, string> DefaultProperties;
private const string Password = "Password99";
public NewUser(
string firstName,
string middleName,
string lastName,
string jobTitle,
string department,
string office,
string role,
string manager)
{
Office = office;
Department = department;
FirstName = firstName.Trim();
MiddleName = middleName.Trim();
LastName = lastName.Trim();
JobTitle = jobTitle.Trim();
Role = role.Trim();
Manager = Ad.GetManagerDistinguishedName(manager);
FullName = $"{FirstName} {LastName}";
Email = $"{FirstName}{LastName}@domain.com";
DotName = $"{FirstName}.{LastName}";
HomeDirectory = $@"\\File\Home$\{DotName}";
try
{
Initials = $"{FirstName[0]}{MiddleName[0]}{LastName[0]}";
}
catch (Exception ex) when (
ex is ArgumentOutOfRangeException ||
ex is NullReferenceException ||
ex is ArgumentNullException)
{
Initials = $"{FirstName[0]}{LastName[0]}";
}
// Set the default properties for AD
DefaultProperties = new Dictionary<string, string>
{
{"userprincipalname", Email},
{"samaccountname", DotName },
{"sn", LastName},
{"givenname", FirstName },
{"displayname", FullName },
{"description", JobTitle },
{"mail", Email },
{"homedirectory", HomeDirectory },
{"homedrive", "H:" },
{"physicalDeliveryOfficeName", Office },
{"Manager", Manager },
{"Initials", Initials }
};
}
public void Create()
{
Ad.CreateUser(this);
Ps.AddExchangeUser(this);
}
}
}
In the constructor, Manager
is initialised with a call to the AD
class, because the manager's DisplayName
is sent through, but for my purposes I need this to be the DsitinguishedName
.
The Create()
method makes a call to Ad
(Active Directory) and Ps
(PowerShell) to do the AD and Exchange creation. I didn't post this code because I thought that it would maybe be a bit much but if people think it would help to put the rest of it in context I will add it!
FirstName
andLastName
never may be null, but could they be empty ? \$\endgroup\$FirstName
andLastName
are made up of some spaces and the form tried to submit - I will have to handle that in the code \$\endgroup\$