Reputation: 199
As an admin, I would like to be able to click a user and make it so that they can't log in, and I tried this, but it didn't seem to work.
Any tips?
public void LockUser(int Id)
{
var UserMan = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = UserMan.FindById(Id);
user.LockoutEnabled = true;
UserMan.Update(user);
}
Upvotes: 3
Views: 877
Reputation: 2840
You are on the right track, but need one thing: The LockoutEnabled should indeed be set to true, but all this does is allow this user to be locked out. You need to actually lock him out. You do this by specifying a lockout end date, so the complete code will look like this:
public void LockUser(int Id)
{
var UserMan = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = UserMan.FindById(Id);
user.LockoutEnabled = true;
user.LockoutEndDateUtc = DateTime.Now.AddDays(14);
UserMan.Update(user);
}
The above code will lock a user out for 2 weeks. See this blog post for more info.
Upvotes: 4