title: ASP.NET MVC LinQ - Use our own method in "Any" LinQ expression?
body: <p>I'm coding a login page in asp.net mvc thanks to multiple articles found on the web but I have a problem to find the matching user in the database.
  Here is my POST method :</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Login([Bind(Include = "username, password")] user model, string returnUrl)
      {
          if (ModelState.IsValid)
          {
              string username = model.username.ToLower();
              string plainPassword = model.password;
  
              bool userValid = db.user.Any(u =&gt; SHA512.VerifyHash(plainPassword, u.salt, u.password) &amp;&amp; u.username_canonical == username);
  
              if (userValid)
              {
                  FormsAuthentication.SetAuthCookie(username, false);
  
                  if (Url.IsLocalUrl(returnUrl) &amp;&amp; returnUrl.Length &gt; 1 &amp;&amp; returnUrl.StartsWith("/") &amp;&amp; !returnUrl.StartsWith("//") &amp;&amp; !returnUrl.StartsWith("/\\"))
                  {
                      return Redirect(returnUrl);
                  }
                  else
                  {
                      return RedirectToAction("Index", "Home");
                  }
              }
              else
              {
                  ModelState.AddModelError("", "The username or password provided is incorrect.");
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>
  
  <p>I got a "NotSupportedException" in this line :</p>
  
  <pre><code>bool userValid = db.user.Any(u =&gt; SHA512.VerifyHash(plainPassword, u.salt, u.password) &amp;&amp; u.username_canonical == username);
  </code></pre>
  
  <blockquote>
    <p>LINQ to Entities does not recognize the method 'Boolean VerifyHash(System.String, System.String, System.String)' method, and this method cannot be translated into a store expression.</p>
  </blockquote>
  
  <p>Here is what in my mind :
  VerifyHash returns true if the provided password (uncrypted) matches with the crypted one stored in database (by getting the salt corresponding to the username, in database and re-encrypting the provided password with this salt, then compare the newly encrypted hash with the stored one).
  Usernames are uniques.</p>
  
  <p>I understand that it doesn't accept my VerifyHash method inside the "Any" one. But I don't know how to do else because I need the corresponding salt and encrypted password (u.salt and u.password) in addition to the matching username to verify the password.</p>
  
  <p>I tried to use the "Find" method in the same way but it doesn't work (VS Error, by memory I need the user ID to pass it as a parameter). I'm new with LinQ to Entities.</p>
  
  <p>Any idea ? Forgive my eventual english mistakes.</p>
  
  <p>Thanks,</p>
  
  <p>Hellcat</p>