Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock/unlock a user in MVC SimpleMembership

In SimpleMembership there isn't any column in the database for a user to be locked/unlocked. I basically need my administrator to enable or disable any user in my application. Is there any alternatives to that?

like image 966
Prabesh Manandhar Avatar asked Nov 06 '12 06:11

Prabesh Manandhar


2 Answers

I find it easiest to use Roles to do this. Have a ActiveUser role and tag your controllers or actions with a Authorize attribute.

[Authorize(Roles = "ActiveUser")]

Then some simple admin to add or remove users from the role to unlock and lock their access to everything protected with that roles attribute.

Roles.AddUserToRole(user.Username, "ActiveUser");

Roles.RemoveUserFromRole(user.Username, "ActiveUser");
like image 179
Iain M Norman Avatar answered Oct 01 '22 13:10

Iain M Norman


Probably not the "approved" way of doing things, but this is how I do it.

There is a field within the webpages_Membership table called IsConfirmed. Typically, this is for when you want a 2-stage registeration process: sign-up then activate via a link within an email. By nature though, this field has the same affect as IsApproved within the former aspnet_Membership table: if set to true, a user can login; if false they can't. So I just use plain old SQL to set to true or false:

// If using EntityFramework
// 1. Setup my params
var params = new List<SqlParameter>() { 
      new SqlParameter("@UserID", 1),
      new SqlParameter("@Activate", true) // or false
};

SqlParameter[] paramArray = params.ToArray();

// 2. Update the database
myDbContext.Database.ExecuteSqlCommand("UPDATE webpages_Membership SET IsConfirmed = @Activate WHERE UserId = @UserID", paramArray);
like image 27
Matt Avatar answered Oct 01 '22 13:10

Matt