public void CustomerRegister(TenantRegisterModel customerRegistration) { string errMessage; if (!ValidModel(customerRegistration, out errMessage)) { throw new BusinessException(errMessage); } Logger.Info("Creating Membership"); MembershipCreateStatus createStatus; var membershipUser = Membership.CreateUser(customerRegistration.UserName, customerRegistration.Password, customerRegistration.UserName, "¿?", "!!", true, Guid.NewGuid(), out createStatus); Logger.Info("Membership created"); var created = false; if (membershipUser != null && membershipUser.ProviderUserKey != null) { try { using (var scope = new TransactionScope()) { using (var db = new AdministrationDb()) { var userId = (Guid)membershipUser.ProviderUserKey; var appUser = new ApplicationUser { UserId = userId, FirstName = customerRegistration.FirstName, LastName = customerRegistration.LastName, EmailAddress = customerRegistration.UserName, IsEmailVerified = true, EmailVerificationToken = string.Empty, IsActive = true, CreatedOn = DateTime.Now, CreatedBy = userId, ModifiedOn = DateTime.Now, ModifiedBy = userId }; var customer = new Customer { CompanyName = customerRegistration.DomainName, Domain = customerRegistration.Domain, DomainName = customerRegistration.DomainName, PhoneNumber = customerRegistration.Phone, IsActive = true, CreatedOn = DateTime.Now, CreatedBy = userId, ModifiedOn = DateTime.Now, ModifiedBy = userId }; var customerUser = new CustomerUser { ApplicationUser = appUser, Customer = customer, IsCreator = true, IsAdmin = true, IsRelationActive = true }; db.ApplicationUsers.Add(appUser); Logger.InfoFormat("User {0} Created", appUser.EmailAddress); db.Customers.Add(customer); Logger.InfoFormat("Customer {0} Created", customer.CompanyName); db.SaveChanges(); Logger.Info("Register Checkpoint"); db.CustomerUsers.Add(customerUser); Logger.Info("Relationship created"); db.SaveChanges(); scope.Complete(); Logger.Info("Transacion Completed"); created = true; } } MailService.SendMail(customerRegistration.UserName, MailMessageType.Welcome); } catch (Exception ex) { if (!created) Membership.DeleteUser(customerRegistration.UserName, true); Logger.Error(ex); } } else { Logger.Error("Membership user couldn't be created"); throw new BusinessException("The user could not be created at this time. Try again soon."); } }
public void UserRegister(ApplicationContext context, RegisterModel model) { using (var db = new AdministrationDb()) { var user = db.ApplicationUsers.FirstOrDefault(x => x.EmailAddress == model.UserName); if (user != null) { throw new BusinessException(""); } Logger.Info("Creating Membership"); MembershipCreateStatus createStatus; var membershipUser = Membership.CreateUser(model.UserName, model.Password, model.UserName, "¿?", "!!", true, Guid.NewGuid(), out createStatus); Logger.Info("Membership created"); var created = false; if (membershipUser != null && membershipUser.ProviderUserKey != null) { var userId = (Guid) membershipUser.ProviderUserKey; var appUser = new ApplicationUser { UserId = userId, FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.UserName, IsEmailVerified = true, EmailVerificationToken = string.Empty, IsActive = true, CreatedOn = DateTime.Now, CreatedBy = context.UserId, ModifiedOn = DateTime.Now, ModifiedBy = context.UserId, }; db.ApplicationUsers.Add(appUser); db.SaveChanges(); var customerUser = new CustomerUser { ApplicationUserId = userId, CustomerId = context.TenantId, IsCreator = false, IsAdmin = false, IsRelationActive = true }; db.CustomerUsers.Add(customerUser); db.SaveChanges(); } } }