New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the Community Exchange, where community members ask and answer questions about DNN. To get started, just start typing your question below and either select one of the suggested questions or ask a new question of your own.

DNN not locking out user programatically

Return to previous page

  • 3/16/2019
  • 1470 Views

Question:

Matthew Erler 6 years ago

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;
}
}
}


Sign In to Participate
Or register to become a member