public ConsumerMember CreateMember(ConsumerRegistrationForm form) { var userName = form.Member.Email.Scrub(); var password = form.Password.Scrub(); var model = ConsumerMember.Empty; if (this.IsMongoMembershipEnabled) { try { var status = MembershipCreateStatus.UserRejected; var membershipUser = this.MembershipProvider.CreateUser(userName, password, userName, "", "", true, null, out status); if (status == MembershipCreateStatus.Success) { // create a user account model = this.Uow.ConsumerMembers.CreateMember(form, membershipUser.ProviderUserKey.ToString()); if (model == null) { throw new Exception("failed to create consumer account"); } } else { Logger.Warn(string.Format("Failed to create Member: {0}. Reason: {1}", userName, status)); } } catch (Exception e) { Logger.Error(string.Format("Error creating Member: {0}", userName), e); throw e; } } else { // create a user account w/o the membership model = this.Uow.ConsumerMembers.CreateMember(form, string.Empty); if (model == null) { throw new Exception("failed to create consumer account"); } } try { WebSecurity.CreateUserAndAccount(userName, password, new { MongoUserId = model.Id }); } catch (Exception e) { Logger.Error(string.Format("Error creating WebSecurity user: {0}", userName), e); throw e; } return(model); }
public HttpResponseMessage RegisterConsumer(ConsumerRegistrationForm registrationForm) { var formValidation = registrationForm.Validate(this.AccountSession, ValidationMode.Create); if (formValidation.IsValid) { var authenticationSecurity = new AuthenticationSecurity() { Uow = this.Uow }; try { // create the account var member = authenticationSecurity.CreateMember(registrationForm); // set the account session (user ID needed for create group) this.Uow.AccountSession = new Model.AccountSession() { MemberId = member.Id }; // use the switchboard to handle any communications related to this new member this.Switchboard.ConsumerMemberCreated(member); // login to the account AuthenticationSecurity.Login(registrationForm.Member.Email, registrationForm.Password); // add appropriate roles to the account var roles = new string[2] { Definitions.Account.Roles.Elevated, Definitions.Account.Roles.Consumer }; authenticationSecurity.AddRolesToUser(registrationForm.Member.Email, roles); // set redirect location based on user type var redirect = AuthenticationSecurity.RegistrationAccountTypeRedirect(AccountType.Consumer); return(CreateSuccessResponse(new { success = true, redirect = redirect }, HttpStatusCode.Created)); } catch (Exception ex) { // log exception Logger.Error(string.Format("Exception detected attempting to create consumer account and login for email {0}", registrationForm.Member.Email), ex); // log the user out AuthenticationSecurity.Logout(); return(CreateErrorResponse(ex)); } } // invalid parameters, generate response return(CreateInvalidResponse(formValidation)); }
public ConsumerMember CreateMember(ConsumerRegistrationForm form, string membershipId) { // create the model var model = new ConsumerMember(form, membershipId); // insert the collection var result = this.Collection.Insert(model); if (result.Ok) { // adjust the receipts to indicate the current user as the creator model.Created = new ChangeReceipt() { On = this.TimeStamp, By = model.Id }; model.Modified = new ChangeReceipt() { On = this.TimeStamp, By = model.Id }; // update with tracking receipts model = Update(model); // log the creation this.Logger.Info(string.Format("created consumer member for email {0} with ID {1}", model.Email, model.Id)); // return the created model return(model); } else { // log the failure this.Logger.Error(string.Format("failed to create consumer member for email {0}. Code: {1}, Reason: {2}", model.Email, result.Code, result.ErrorMessage)); } return(null); }