public async Task <ActionResult> Register(AppUserRegisterVm appUserRegisterVm) { var result = View(); // If registerVm is null, then return the empty registration form if (appUserRegisterVm == null) { return(result); } if (appUserRegisterVm.AcceptTermsOfService == false) { ModelState.AddModelSummaryError("You need to read and accept the Terms of Service."); } // If model is valid, continue with the registration if (ModelState.IsValid) { // Register the user through the service. var stggResult = await Managers.UserAccountManager.RegisterAsync(appUserRegisterVm); if (stggResult.Status == StggResultStatus.Succeeded) { StggSecurityContext.SetCurrentUser(stggResult.Value); return(RedirectToAction("EmailConfirmTokenSent")); } else { ModelState.AddModelSummaryError(stggResult.Errors); } } // Model is invalid, bring the user back to the page. return(View(appUserRegisterVm)); }
/// <summary> /// Creates a very basic profile instance for a registering user. /// </summary> /// <param name="appUserRegisterVm">User registration view model that contains the first name and the last name of the user.</param> /// <param name="defaultAvatar">Default user avatar.</param> internal static UserProfile BuildOneUserProfile_ByAppUserRegisterVm(AppUserRegisterVm appUserRegisterVm, string defaultAvatar = "") { var userProfile = new UserProfile { FirstName = appUserRegisterVm.FirstName, LastName = appUserRegisterVm.LastName, SubscribeNewsletter = appUserRegisterVm.SubscribeNewsletter }; return(userProfile); }
/// <summary> /// Build a User object isntance used for registring a new user. /// </summary> /// <param name="appUserRegisterVm">User registration view model that contains the user name and the password.</param> /// <param name="userProfile">User profile of this user.</param> internal static User BuildOneUser_ByAppUserRegisterVm(AppUserRegisterVm appUserRegisterVm, UserProfile userProfile) { return(new User { UserName = appUserRegisterVm.UserName, Email = appUserRegisterVm.Email, AccountStatus = AccountStatus.Pending, RegisterDate = DateTime.UtcNow, UserProfile = userProfile }); }
/// <summary> /// Create a new user account. /// </summary> /// <param name="appUserRegisterVm">Registration view model.</param> public async Task <StggResult <AppUserVm> > RegisterAsync(AppUserRegisterVm appUserRegisterVm) { // Make sure that the email isn't being used by an existing user. var stggResult = new StggResult <AppUserVm>(); var userByEmail = await AppUserManager.FindByEmailAsync(appUserRegisterVm.Email); if (userByEmail != null) { stggResult.AddError("The specified email address already exist within the system. Please enter a different email address."); return(stggResult); } // Create data models from the register view model. var userProfile = SecurityFactory.BuildOneUserProfile_ByAppUserRegisterVm( appUserRegisterVm, AppSettings.DefaultUserAvatar); var user = SecurityFactory.BuildOneUser_ByAppUserRegisterVm( appUserRegisterVm, userProfile); // Save changes to the database. var createResult = await AppUserManager.CreateAsync(user, appUserRegisterVm.Password); if (createResult.Succeeded == false) { stggResult.AddError("Failed to create user account."); return(stggResult); } // Add user to the User Role var addRoleResult = await AppUserManager.AddToRoleAsync(user.Id, Role.NAME_USER); if (addRoleResult.Succeeded == false) { stggResult.AddError("Failed to create user account."); return(stggResult); } // Get the application user instance. var stggResUser = (await FindByIdAsync(user.Id)).Value; stggResUser.EmailConfirmationToken = GenerateEmailConfirmationToken(stggResUser.Id); // Set the stgg output stggResult.SetValue(stggResUser); // Return the application user. return(stggResult); }