Esempio n. 1
0
        public async Task <VerifiedUserCreatedContract> CreateVerifiedUserAsync(CreateVerifiedUserContract contract)
        {
            var fullPath = $"{BasePath}createVerified";

            return(await m_authorizationServiceHttpClient.SendRequestAsync <VerifiedUserCreatedContract>(HttpMethod.Post, fullPath,
                                                                                                         contract));
        }
        public async Task <ActionResult> CreateVerifiedUserAsync([FromBody][Required] CreateVerifiedUserContract createUserContract)
        {
            if (m_logger.IsEnabled(LogLevel.Information))
            {
                m_logger.LogInformation(GetMethodCalledLoggingString(null, createUserContract));
            }

            var userModel = Mapper.Map <UserModel>(createUserContract.User);

            userModel.Username = string.IsNullOrEmpty(createUserContract.UserName)
                ? m_userManager.GenerateUsername().Result
                : createUserContract.UserName;

            var appUser = Mapper.Map <ApplicationUser>(userModel);

            var password = await GeneratePasswordForUserAsync(appUser);

            if (password == null)
            {
                return(Error(m_translator.Translate(m_translator.Translate("generate-password-failed"),
                                                    DataResultErrorCode.GeneratePassword)));
            }

            appUser.EmailConfirmCode = await m_identityUserManager.GenerateEmailConfirmationTokenAsync(appUser);

            appUser.PhoneNumberConfirmCode = await m_identityUserManager.GeneratePhoneConfirmationTokenAsync(appUser);

            var result = await m_identityUserManager.CreateAsync(appUser, password);

            if (!result.Succeeded)
            {
                var error = result.Errors.FirstOrDefault();
                return(Error(error?.Description, error?.Code));
            }

            var user = m_identityUserManager.FindByNameAsync(appUser.UserName).Result;

            m_userManager.AddRoleToUser(user.Id, RoleNames.VerifiedUser);

            _ = m_identityUserManager.SendConfirmContactsCodesAsync(user); //Ignore error, user can use Resend button

            var userResult = m_userManager.GetUserByUsername(appUser.UserName).Result;

            userResult.VerificationCode = null;
            m_userManager.UpdateUser(userResult.Id,
                                     userResult); //TODO Why is here used this method instead of identity method for user update?

            var userContract = Mapper.Map <VerifiedUserCreatedContract>(userResult);

            userContract.Password = password;

            return(Json(userContract));
        }