Beispiel #1
0
        public DataAccessResponse <string> CreateUser(Dto.ApplicationUser userDto, string password, bool isAuthor)
        {
            var resp = new DataAccessResponse <string> {
                ResponseCode = ResponseCode.Fail
            };

            bool userExists = true;

            if (string.IsNullOrEmpty(userDto.Email))
            {
                userExists = _db.Users.Any(u => u.UserName == userDto.UserName);
            }
            else
            {
                userExists = _db.Users.Any(u => u.UserName == userDto.UserName || u.Email == userDto.Email);
            }

            if (userExists)
            {
                resp.ResponseMessage = ErrorMessage.UserExists;
                return(resp);
            }

            var userModel = new ApplicationUser
            {
                NameSurname      = userDto.NameSurname,
                Email            = userDto.Email ?? "",
                EmailConfirmed   = userDto.EmailConfirmed,
                PhoneNumber      = userDto.PhoneNumber,
                UserName         = userDto.UserName,
                Status           = userDto.Status,
                TwoFactorEnabled = userDto.ContactPermission, // contact permission field matched with twoFactorEnabled column
                PasswordHash     = HashPassword(password),
                SecurityStamp    = Guid.NewGuid().ToString()
            };

            var newUser = _db.Users.Add(userModel);

            //if (isAuthor)
            //{
            //    var role = new IdentityUserRole
            //    {
            //        UserId = newUser.Id,
            //        RoleId = DatabaseKey.ApplicationRoleId.Author
            //    };
            //    newUser.Roles.Add(role);
            //}
            _db.SaveChanges();

            resp.ResponseCode = ResponseCode.Success;
            resp.ResponseData = newUser.Id;

            return(resp);
        }
        public void Save()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat("Entity Validation:", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }