public static MembershipUserWrapper CreateFromRegistration(Registration registration) { Boolean exists = (System.Web.Security.Membership.GetUser(registration.Email) != null); MembershipUserWrapper wrapper = new MembershipUserWrapper(); if (String.IsNullOrEmpty(registration.ExistingAccountGuid)) { UserInfo info = new UserInfo(); MembershipUser user = null; info.Username = registration.Email; info.Email = registration.Email; info.Firstname = registration.Firstname; info.Lastname = registration.Lastname; info.Company = registration.Company; info.Address1 = registration.Address1; info.Address2 = registration.Address2; info.City = registration.City; info.State = registration.State; info.Zipcode = registration.Zipcode; info.Guid = System.Guid.NewGuid().ToString(); info.Created = UtcDateTime.Now; UserInfoDao dao = new UserInfoDao(); if (!exists) { using (Transaction tx = new Transaction()) { dao.SaveObject(info); tx.Commit(); } } //Create the account in the asp.net membership system String password = Decrypt(registration.EncryptedPassword); try { if (!exists) user = System.Web.Security.Membership.CreateUser(registration.Email, password, registration.Email); if (!System.Web.Security.Roles.IsUserInRole(registration.Email, SecurityConstants.Roles.SITE_ADMINISTRATOR)) System.Web.Security.Roles.AddUserToRole(registration.Email, SecurityConstants.Roles.SITE_ADMINISTRATOR); } catch (MembershipCreateUserException e) { //Rollback the user info using (Transaction tx = new Transaction()) { dao.DeleteObject(info); tx.Commit(); } throw e; } wrapper.MembershipUser = user; wrapper.UserInfo = info; } else { UserInfo info = new UserInfoDao().FindByGuid(registration.ExistingAccountGuid); if (info != null) { //make sure the email addresses match if (info.Email.EqualsCaseInsensitive(registration.Email)) wrapper = FindByUsername(info.Email); } } return wrapper; }
/// <summary> /// Finds the membership information based upon the username /// </summary> /// <param name="p"></param> /// <returns></returns> public static MembershipUserWrapper FindByUsername(String username) { MembershipUser user = System.Web.Security.Membership.GetUser(username); UserInfo info = new UserInfoDao().FindByUsername(username); //Check if the asp.net membership account is orphaned, if so, delete it if ((user != null) && (info == null)) { System.Web.Security.Membership.DeleteUser(user.UserName); user = null; } //Check if the gooeycms account is orphaned, if so, delete it if ((user == null) && (info != null)) { UserInfoDao dao = new UserInfoDao(); using (Transaction tx = new Transaction()) { dao.Delete<UserInfo>(info); tx.Commit(); } info = null; } MembershipUserWrapper wrapper = new MembershipUserWrapper(); wrapper.MembershipUser = user; wrapper.UserInfo = info; return wrapper; }
public static MembershipUserWrapper CreateDemoAccount() { UserInfo info = new UserInfo(); info.Username = DemoAccountUsername; info.Email = DemoAccountUsername; info.Firstname = "GooeyCMS Demo"; info.Lastname = "Account"; info.Company = "GooeyCMS"; info.Address1 = "135 Gooey Ave"; info.Address2 = null; info.City = "New York"; info.State = "NY"; info.Zipcode = "10114"; info.Guid = System.Guid.NewGuid().ToString(); info.Created = UtcDateTime.Now; UserInfoDao dao = new UserInfoDao(); using (Transaction tx = new Transaction()) { dao.Save<UserInfo>(info); tx.Commit(); } MembershipUser user = System.Web.Security.Membership.CreateUser(DemoAccountUsername, DemoAccountPassword, DemoAccountUsername); System.Web.Security.Roles.AddUserToRole(DemoAccountUsername, SecurityConstants.Roles.SITE_ADMINISTRATOR); MembershipUserWrapper wrapper = new MembershipUserWrapper(); wrapper.MembershipUser = user; wrapper.UserInfo = info; return wrapper; }