I'm using DNN 7 and trying to lock user accounts programmatically from a scheduled job. The code that I have written evaluates all users in all portals and if a user doesn't exist in Active Directory or if the user does exist in Active Directory but the ID is disabled then the code should lock out the ID in DNN. The code that I'm using to lock a user's account is:
portalUser.Membership.LockedOut = true;
UserController.UpdateUser(portalInfo.PortalID, portalUser);
A user's account that should be locked out isn't according to information about the user's account visible on Users-->Manage Users--> Edit UserAccounts page and I don't know why. Please help. Here is the code that I'm using:
public class UnauthorizeInvalidUsersJob : SchedulerClient
{
public override void DoWork()
{
try
{
Progressing();
ArrayList allPortals = PortalController.Instance.GetPortals();
foreach (PortalInfo portalInfo in allPortals)
{
ArrayList allPortalUsers = UserController.GetUsers(portalInfo.PortalID);
foreach (UserInfo portalUser in allPortalUsers)
{
bool isMsId = portalUser.Username.StartsWith("MS\\", StringComparison.OrdinalIgnoreCase);
if (isMsId && !portalUser.Membership.LockedOut)
{
try
{
DirectoryUser activeDirectoryUser =
_activeDirectory.GetUserByUserId(
portalUser.Username.Replace("MS\\", String.Empty));
if (activeDirectoryUser != null)
{
if (activeDirectoryUser.Disabled)
{
portalUser.Membership.LockedOut = true;
UserController.UpdateUser(portalInfo.PortalID, portalUser);
}
}
else // DNN ID not found in Active Directory. Lock the DNN ID out.
{
portalUser.Membership.LockedOut = true;
UserController.UpdateUser(portalInfo.PortalID, portalUser);
}
}
catch (CloudSdkInvalidUseException exc)
{
// Log the exception
}
}
}
ScheduleHistoryItem.Succeeded = true;
}
}
catch (Exception exc)
{
// Log the exception
ScheduleHistoryItem.Succeeded = false;
}
}
}