Example #1
0
        /// <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);
        }