postgresql 可以使用Npgsql将C# byte[]转换为postgressql bytea吗

k4ymrczo  于 2023-03-08  发布在  PostgreSQL
关注(0)|答案(1)|浏览(205)

所以我在postgres中将byte[]转换为bytea时遇到了一个问题。我有一个程序,它接收一个密码(字符串),使用HMACSHA512将其转换为byte []。我想将我的byte[]数据插入到我的postgres数据库中。
插入声明

public static int Insert(User user)
    {
      string sqlcommand = $"INSERT INTO \"{Settings.UserTable}\" (userName, surname, email, passwordhash, passwordsalt, status) VALUES (@name, @surname, @email, encode(E'@passwordhash'::bytea, 'escape'), @passwordsalt::bytea, 0::bit)";
      using (NpgsqlCommand command = new NpgsqlCommand(sqlcommand, DatabaseConnection.Connection))
      {

        command.Parameters.AddWithValue("name", user.Name);
        command.Parameters.AddWithValue("surname", user.Surname);
        command.Parameters.AddWithValue("email", user.Email);
        command.Parameters.AddWithValue("passwordhash", user.PasswordHash);

        int result = (int)command.ExecuteNonQuery();
        return result;
      }
    }

型号

public class User
    {
        [Required]
        public string Email { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Surname { get; set; }
        [Required]
        public byte[] PasswordSalt { get; set; }
        [Required]
        public byte[] PasswordHash { get; set; }
        
        public int status { get; set; }

        public string Token { get; set; }
        public DateTime TokenCreated { get; set; }
        public DateTime TokenExpires { get; set; }

        public User()
        {
            
        }

        public User(string name, string surname, string email, byte[] passwordHash, byte[] passwordSalt)
        {
            this.Name = name;
            this.Surname = surname;
            this.Email = email;
            this.PasswordHash = passwordHash;
            this.PasswordSalt = passwordSalt;
        }

这是我的控制器

[HttpPost("Register")]
        public IActionResult Register([FromBody] Register model)
        {
            var user = new User { Name = model.Name, Surname = model.Surname, Email = model.Email};

            using (HMACSHA512? hmac = new HMACSHA512())
            {
                user.PasswordSalt = hmac.Key;
                user.PasswordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(model.Password));
            }

            UserQuery.Insert(user);

            return Ok(user);
        }

我的问题是它一直给我这个错误

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: Parameter 'name' must have either its NpgsqlDbType or its DataTypeName or its Value set

我假设这与passwordHash和passwordSalt的类型转换有关

hmtdttj4

hmtdttj41#

该错误表明@name参数为null,因此Npgsql不知道要发送哪种类型。如果目的确实是插入null名称,则可以在NpgsqlParameter上设置NpgsqlDbType。
请注意,以下操作无效:encode(E'@passwordhash'::bytea, 'escape');您不能以这种方式将参数占位符放入字符串中。如果@passwordhash在.NET中是byte[],并且您希望将其传递给PG encode,则只需执行encode(@passwordhash, 'escape')即可。

相关问题