I'm adding a User (custom class) through my Repository by hijacking the generic Add(T item) method and throwing it to a private AddUser method, if item == typeof(User). It does just that, but as soon as it hits the FlushMembership() call, it exits out too early, ie. it doesn't hit anything beyond that FlushMembership() call and thus no User gets added. It steps through everything in FlushMembership though. I don't get it.
private void AddUser(User u)
{
var existingUser = Membership.GetUser(u.Username);
// TODO: This could be more elegant
FlushMembership(); // Clean out this crap
var mU = existingUser;
if (mU == null)
{
Membership.CreateUser(u.Username, u.Password, u.Email);
CKDClientAreaEntities.Current.AddObject(GetSetName<User>(), u);
}
else
mU = Membership.GetUser(u.Username);
if(Roles.GetRolesForUser(u.Username).Count() <= 0)
Roles.AddUserToRole(mU.UserName, u.Role);
Membership.UpdateUser(mU);
}
private void FlushMembership()
{
var allMembers = Membership.GetAllUsers();
var allRoles = Roles.GetAllRoles();
foreach(var r in allRoles)
{
var ms = Roles.GetUsersInRole(r);
foreach(var m in ms)
{
var u = Users.Single(o => o.Username == m);
if(u == null)
Roles.RemoveUserFromRoles(m, allRoles); // If the user doesn't exist, remove them from all roles!
}
}
foreach(var m in allMembers)
{
var u = Users.Single(o => o.Username == m);
if (u == null)
Membership.DeleteUser(u.Username);
}
}