1

Im doing something like this to get a list of all users that have been blocked:

public IQueryable<Aspnet_User> GetBannedUsers()
{
    return db.Aspnet_Memberships.Where(x => x.IsApproved == false).Select(x => x.Aspnet_User);
}

Im doing this to block users:

MembershipUser user = Membership.GetUser(username);
if (user != null)
{
    user.IsApproved = isApproved;
    Membership.UpdateUser(user);
}

how can i get the list of banned users ordered by date they were banned. most recently banned should be first.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
raklos
  • 28,027
  • 60
  • 183
  • 301

2 Answers2

2
public IQueryable<Aspnet_User> GetBannedUsers()
{
    return db.Aspnet_Memberships
             .Where(x => x.IsApproved == false)
             .OrderByDescening(x => x.BannedDate) //adjust property here
             .Select(x => x.Aspnet_User);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • im using sql membership provider so where do i introduce banneddate? should i add it directly to the table Aspnet_Memberships or is that bad practise messing withe the "out of the box" tables? – raklos Feb 27 '11 at 14:24
  • No idea, I thought this was included? I would introduce a new table with the membership ID and the BannedDate. – Femaref Feb 27 '11 at 14:31
2

By default, asp.net membership does not store this date, so you'll have to use a workaround. A neat approach would be to introduce your own table, but I suggest abusing the LastLoginDate. The default mechanism will not touch this date as long as the user doesn't login properly (which your !isApproved will prevent from happening).

Modified code to block user:

MembershipUser user = Membership.GetUser(username);             
if (user != null)             
{                 
  user.IsApproved = isApproved;
  user.LastLoginDate = DateTime.Now;
  Membership.UpdateUser(user);             
}

Code to sort on banned date, copy pasted from @Femaref's answer:

public IQueryable<Aspnet_User> GetBannedUsers()            
{ 
  return db.Aspnet_Memberships          
    .Where(x => x.IsApproved == false)                        
    .OrderByDescening(x => x.LastLoginDate) 
    .Select(x => x.Aspnet_User);
}
Paul-Jan
  • 16,746
  • 1
  • 63
  • 95