I am new to the whole LINQ system, i have my passwords hashed and stored in a varbinary field in my database, now i want to get that value from my database and store it in a byte array to do a comparison using LINQ. I did it like this before:
System.Data.SqlTypes.SqlBytes sqlPassBinary = dr.GetSqlBytes(dr.GetOrdinal("Password"));
Now i want to try and apply this same concept but using LINQ this time. I tried this but it didn't work:
public bool Authenticate(string user, string pass)
{
***LINQDataContext d = new ***LINQDataContext();
var login = from us in d.Users
join ur in d.UserRoles on us.UserRoleID equals ur.UserRoleID
where us.Username == user
select us;
if ((login as IEnumerable<object>).Any())
{
System.Web.HttpContext.Current.Session["UserID"] = login.FirstOrDefault().UserID.ToString();
System.Web.HttpContext.Current.Session["UserRole"] = login.FirstOrDefault().UserRole.ToString();
byte[] sqlbinary = (byte[]) login.FirstOrDefault().Password;;
return true;
}
return false;
}
And i get this error:
This error indicates that the collection returned by LINQ is a System.Data.Linq.Binary
object and you cannot use it to initialize the byte[]
, nor to convert it explicitly.
First, you need to convert it to the array, use the LINQ's built-in ToArray()
function.
Use
byte[] sqlbinary = login.FirstOrDefault().Password.ToArray();
It should be just fine.