Вопрос

I'm working with Entity Framework Code First Migrations and I'd like to have the Seed method in my Configuration.cs file create a default user for me. I'm storing the hashed user passwords in the database as varbinary(64) and the salt as varbinary(16). I copied the hash and salt of an existing user so the seed method can use it when creating the default user. The problem I'm having is converting the string representation of the password hash and salt into their corresponding sql data types. Here's what I've tried so far:

string hashString = "0x81E09FC75CFAB13F54DF1266ADCA53B9FAE45C1D80655C61DE88057846F9B61DC3ED257F2C7D7B73826F9DC0FFA    5FF987B1A594FD9DAE3DC492F5815E989CD34";
string saltString = "0x630FE0A0186365FF9CCBB0FA6161C08B";

Byte[] pbytes = Encoding.ASCII.GetBytes(hashString);
Byte[] sbytes = Encoding.ASCII.GetBytes(saltString);

context.Users.AddOrUpdate(p => p.FirstName,
                new User
                {
                    Id = Guid.NewGuid(),
                    FirstName = "John",
                    LastName = "Doe",
                    Username = "admin",
                    Email = "jdoe@test.com",
                    PasswordHash = pbytes,
                    PasswordSalt = sbytes,
                    RoleId = 1,
                    LastLoginDate = null
                });

When I run update-database from the Package Manager Console, I get a "Validation failed for one or more entities" error. Just to test things, I copied my hashing code over and tried using that and it worked as expected. Obviously, I don't want to reuse this code in my seed method so I just need a way to convert the strings int Byte[] that the db will be happy with. I've included the code that works below.

  byte[] saltBytes = new byte[16];
  RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  rng.GetNonZeroBytes(saltBytes);
  byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes("admin");
  byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
  plainTextBytes.CopyTo(plainTextWithSaltBytes, 0);
  saltBytes.CopyTo(plainTextWithSaltBytes, plainTextBytes.Length);
  var hash = new SHA512Managed();
  byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

context.Users.AddOrUpdate(p => p.FirstName,
            new User
            {
                Id = Guid.NewGuid(),
                FirstName = "John",
                LastName = "Doe",
                Username = "admin",
                Email = "jdoe@test.com",
                PasswordHash = hashBytes,
                PasswordSalt = saltBytes,
                RoleId = 1,
                LastLoginDate = null
            });
Это было полезно?

Решение

Looks like you are trying to convert hexadecimal to Bytes not String to bytes. So you want to start here.... How can I convert a hex string to a byte array?

but if you end up with a "normal" string to begin with...

    public static byte[] Base64StringToByteArray(string base64String) {
        return Convert.FromBase64String(base64String);
    }


    public static byte[] UnicodeStringToByteArray(this string source) {
        return Encoding.Unicode.GetBytes(source);
    }
    public static byte[] UTF8StringToByteArray(this string source)
    {
        return Encoding.UTF8.GetBytes(source);
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top