Create a new login and assign it to the Administrators role
This sample demonstrates how to create a new adTempus login for a Windows user and assign it to a role (security group).
The code creates a new adTempus login for a Windows security principal (or group), then assigns the login to the Administrators role in adTempus.
public void Main()
{
using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
{
using (var context = session.NewDataContext())
{
var login = CreateLoginForUser(context, @"domain\username");
if (login == null)
return;
// now assign the user to the Administrators group (role)
// fetch the role
var adminRole = context.GetSecurityRole("Administrators");
if (adminRole == null)
return;
// add the role to the user and save
login.Roles.Add(adminRole);
login.Save();
}
}
}
private SecurityLogin CreateLoginForUser(ArcanaDevelopment.adTempus.Client.DataContext context, string windowsLoginName)
{
var login = context.GetSecurityLogin(windowsLoginName);
if (login != null)
{
// login already exists
return null;
}
// no login exists for this name. Need to create one.
// look up the user in Active Directory to get the Windows SID
var sid = context.Scheduler.ResolveSecurityName(windowsLoginName);
if (sid == null)
{
// user not found on server
// handle error
return null;
}
// create the login
login = (SecurityLogin)context.CreateObject(ClassID.SecurityLogin);
// if the Windows identity is a user, configure as a user in adTempus
if (sid.NameUse == SIDNameUse.User)
{
login.LoginType = LoginType.WindowsUser;
}
else
{
// otherwise it's a group. Configure it as a group in adTempus to make it a template login
// (see Automatic Login through Group Membership
login.LoginType = LoginType.WindowsGroup;
}
login.WindowsSid = sid.SIDString;
login.Name = windowsLoginName;
login.Save();
return login;
}