1

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: enter image description here

2
  • 1
    what is login? you need to show a full code example with all your members. Commented May 14, 2014 at 9:49
  • 5
    "it didn't work" tells us very little about what actually happened. Did it fail to compile? Did it compile but give an execution-time exception? Did it silently give you incorrect data? Please give more information. (Think about what you'd want to know if someone came to you with the question and asked you to help solve it.) Commented May 14, 2014 at 9:50

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.