Question

I want administrators on my site to be capable of locking members out, but the IsLockedOut() for the default mvc membership is readonly. I read that I need to create a custom membership provider, but I am not entirely sure what that means. Do I need to create a new model with a membershipUser property? If possible, I don't want to create a new table in the database.

Here is the code that I have for my lockUser method:

public static void LockUser(this MembershipUser user)
    {
        using (var db = new MovieContext())
        {
            Guid userid = (Guid)user.ProviderUserKey;
            MembershipUser member = Membership.GetUser(userid);
            member.IsLockedOut = true;
            member.LastLockoutDate = DateTime.Now;
            db.SaveChanges();
        }
    }
Était-ce utile?

La solution

Andrew, try this approach. It uses IsApproved rather than the IsLockedOut. If your implementation does not already use IsAproved, this would be a good solution.

MembershipUser muUser = Membership.GetUser(strUsernameToActOn);
muUser.IsApproved = false;
Membership.UpdateUser(muUser);

This is not exactly locking the user. Technically this call is taking approved status from the user and leaving them unable to log-in.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top