Esempio n. 1
0
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> CreateStandardUser(UserDTO userdata)
        {
            // TODO: hardcoded standard user role name!
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var userresult = CreateUserAndMapToEntity(userdata);
                if (userresult.Errors.Count() > 0)
                {
                    return(false, userresult.Errors);
                }

                var createresult = await _userManager.CreateAsync(userresult.User, userdata.PasswordClear);

                if (!createresult.Succeeded)
                {
                    return(false, createresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                userresult.User.RegisteredTimestamp = DateTime.UtcNow;
                var updateresult = await _userManager.UpdateAsync(userresult.User);

                if (!updateresult.Succeeded)
                {
                    return(false, updateresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                var rolesresult = await this._userManager.AddToRolesAsync(userresult.User, new[] { "User" });

                if (!rolesresult.Succeeded)
                {
                    return(false, rolesresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                var confirmmailresult = await _useremailmanager.GenerateEmailConfirmation(userresult.User.Id);

                if (!confirmmailresult.Succeeded)
                {
                    return(false, new List <FieldValidationErrorDTO>()
                    {
                        new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(confirmmailresult.Error.Key), confirmmailresult.Error.Description)
                    });
                }

                scope.Complete();
                return(true, new List <FieldValidationErrorDTO>());
            }
        }
        public async Task <IActionResult> SendEmailConfirmation(string userid)
        {
            var result = await _userEmailManager.GenerateEmailConfirmation(userid);

            if (!result.Succeeded)
            {
                return(CreateBadRequestError(result.Error.Key, result.Error.Description));
            }
            return(StatusCode(201));
        }