Esempio n. 1
0
        public ActionResult RegisterUser([FromBody] User entity)
        {
            try
            {
                if (!Regex.IsMatch(entity.Email, "^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$"))
                {
                    return(ExceptionHandler.throwException("\"" + entity.Email + "\" is not a valid email"));
                }
                if (!Regex.IsMatch(entity.Password, "^.*(?=.{8,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
                {
                    return(ExceptionHandler.throwException("\"" + entity.Password + "\" is not a valid password. It should be 8 characters or more and should contain atleast a Digit, a Lowercse letter, a Uppercase letter and a Special character."));
                }
                if (!Regex.IsMatch(entity.PhoneNumber, "^[6-9]{1}[0-9]{9}$"))
                {
                    return(ExceptionHandler.throwException("\"" + entity.PhoneNumber + "\" is not a valid phone number."));
                }
                if (string.IsNullOrWhiteSpace(entity.Title))
                {
                    return(ExceptionHandler.throwException("Name should not be empty."));
                }

                var existingUser = FindByEmail(entity);
                if (existingUser != null && existingUser.Count > 0)
                {
                    return(ExceptionHandler.throwException("Email \"" + entity.Email + "\" is already taken"));
                }

                byte[] passwordHash, passwordSalt;
                EncryptionManager.ComputeHashForPassword(entity.Password, out passwordHash, out passwordSalt);

                entity.PasswordHash = passwordHash;
                entity.PasswordSalt = passwordSalt;

                var entityAdded = genericRepository.Add(entity);
                entityAdded.Password     = null;
                entityAdded.PasswordHash = null;
                entityAdded.PasswordSalt = null;

                GenerateTokenString(entityAdded, out string tokenString);

                SetCache(tokenString, entityAdded);

                return(Json(new
                {
                    ProfileDetails = entityAdded,
                    TokenString = tokenString
                }));
            }
            catch (Exception ex)
            {
                return(ExceptionHandler.throwException(ex.Message));
            }
        }