Inserting a aspnet_membership user directly into the database

39 Views Asked by At

I was asked to seed an application through the database, while doing so I tried to add the initial user to the database Membership table, here is how I did it

DECLARE @password NVARCHAR(128) = 'P@ssw0rd123';
DECLARE @salt UNIQUEIDENTIFIER = NEWID(); 
DECLARE @toHash NVARCHAR(MAX) = CONCAT(@password, CAST(@salt AS NVARCHAR(36)));
DECLARE @hashedPassword VARBINARY(64) = HASHBYTES('SHA2_256', @toHash);
DECLARE @UserId UNIQUEIDENTIFIER = NEWID();
DECLARE @ApplicationId UNIQUEIDENTIFIER = (SELECT ApplicationId 
                                           FROM aspnet_Applications 
                                           WHERE ApplicationName = @ApplicationName);

INSERT INTO aspnet_Membership (ApplicationId, UserId, Password, PasswordSalt, 
                               IsApproved, IsLockedOut, CreateDate, LastLoginDate, 
                               LastPasswordChangedDate, LastLockoutDate, 
                               FailedPasswordAttemptCount, FailedPasswordAttemptWindowStart,
                               FailedPasswordAnswerAttemptCount, FailedPasswordAnswerAttemptWindowStart)
VALUES (@ApplicationId, @UserId, @hashedPassword, @salt, 
        1, 0, GETDATE(), GETDATE(), 
        GETDATE(), '17540101', 0, GETDATE(), 0, GETDATE()); 

But after I try to select the record I see the password in Chinese Letters, and I will always get the message for the wrong password when logging in.

I tried using CONVERT to make the hashed string hexadecimal, the hashed string was no longer Chinese but I still get the wrong password message

0

There are 0 best solutions below