My User table contains PasswordHash and PasswordSalt fields. I'm trying to do a lookup of a User based on their username and unhashed password. Since the salt is in the db I need to do the sha1 of the salt and password in the query.
How do I go about doing this? I was thinking I could create a new IEntityField2 and pass it a DbFunctionCall, but how would I pass UserFields.PasswordSalt into this to do the concatenation?
Is there another/better way of doing this?
Edit: ok, I've kind of got this working. It's generating the sql correctly and i'm able to copy that sql to managment studio and run the query and get the expected result. However, the EntityCollection isn't returning anything.
EntityCollection<UserEntity> col = new EntityCollection<UserEntity>();
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
IEntityField2 passwordField = new EntityField2("Password", new DbFunctionCall("SubString(master.dbo.fn_varbintohexstr(HashBytes('SHA1', {0} + {1})), 3, 40)", new object[] {password,UserFields.PasswordSalt}));
IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(UserFields.Username == username);
bucket.PredicateExpression.AddWithAnd(UserFields.PasswordHash == passwordField);
adapter.FetchEntityCollection(col, bucket, 1);
}
Query: SELECT TOP 1 [Platform].[dbo].[User].[ID] AS [Id], [Platform].[dbo].[User].[AccountID] AS [AccountId], [Platform].[dbo].[User].[Username], [Platform].[dbo].[User].[PasswordHash], [Platform].[dbo].[User].[PasswordSalt], [Platform].[dbo].[User].[ContactID] AS [ContactId], [Platform].[dbo].[User].[UserType], [Platform].[dbo].[User].[IsLocked], [Platform].[dbo].[User].[DateCreated], [Platform].[dbo].[User].[DateModified], [Platform].[dbo].[User].[DatePasswordChanged], [Platform].[dbo].[User].[Status] FROM [Platform].[dbo].[User] WHERE ( ( [Platform].[dbo].[User].[Username] = @Username1 AND [Platform].[dbo].[User].[PasswordHash] = SubString(master.dbo.fn_varbintohexstr(HashBytes('SHA1', @LO1c5483dd2 + [Platform].[dbo].[User].[PasswordSalt])), 3, 40)))
Parameter: @Username1 : AnsiString. Length: 100. Precision: 0. Scale: 0. Direction: Input. Value: "wbrowning".
Parameter: @LO1c5483dd2 : String. Length: 8. Precision: 0. Scale: 0. Direction: Input. Value: "testpassword".