Languages: C#, VB.NET
View on GitHub to download or comment.
See the Client API Examples Introduction for additional information and prerequisites.
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;
}
Sub Main
Using session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", "")
Using context = session.NewDataContext()
Dim login = CreateLoginForUser(context, "domain\username")
If login Is Nothing Then
Return
End If
'now assign the user to the Administrators group (role)
'fetch the role
Dim adminRole = context.GetSecurityRole("Administrators")
If adminRole Is Nothing Then
Return
End If
'add the role to the user and save
login.Roles.Add(adminRole)
login.Save()
End Using
End Using
End Sub
Private Function CreateLoginForUser(context As ArcanaDevelopment.adTempus.Client.DataContext, windowsLoginName As String) As SecurityLogin
Dim login = context.GetSecurityLogin(windowsLoginName)
If login IsNot Nothing Then
'login already exists
Return Nothing
End If
'no login exists for this name. Need to create one.
'look up the user in Active Directory to get the Windows SID
Dim sid = context.Scheduler.ResolveSecurityName(windowsLoginName)
If sid Is Nothing Then
'user not found on server
'handle error
Return Nothing
End If
'create the login
login = CType(context.CreateObject(ClassID.SecurityLogin),SecurityLogin)
'if the Windows identity is a user, configure as a user in adTempus
If sid.NameUse = SIDNameUse.User Then
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
End If
login.WindowsSid=sid.SIDString
login.Name = windowsLoginName
login.Save()
Return login
End Function
View on GitHub to comment.