(MVC)LINQ to Entities无法识别方法'Boolean VerifyHashedPassword(Byte [],System.String)'方法

时间:2017-01-16 08:54:18

标签: c# entity-framework linq

当我运行我的代码并且在这里发生错误时。任何人都可以帮我这个吗?我是MVC的新手。谢谢。

public ActionResult Login(AspNetUser user)
        {
            using (eMediCareDBEntities db = new eMediCareDBEntities())
            {
                //byte[] temp = Convert.FromBase64String("AEh/kUSTE019aDWRoSscnT0c/XArWnjMyBeIgQ1MgTAqRetiD84KgdkFAgHO/bfFKQ==");

                //bool result = VerifyHashedPassword(temp, user.PasswordHash);

                var v = db.AspNetUsers.Single(a => a.UserName.Equals(user.UserName) && VerifyHashedPassword(Convert.FromBase64String(a.PasswordHash), user.PasswordHash) == true);
                if(v != null)
                {
                    Session["Id"] = v.Id.ToString();
                    Session["UserName"] = v.UserName.ToString();
                    return RedirectToAction("Main");
                }
                else
                {
                    ModelState.AddModelError("", "Username or Password is wrong.");
                }
            }
            return View();
        }

错误是:

  

EntityFramework.SqlServer.dll中发生了'System.NotSupportedException'类型的异常,但未在用户代码中处理

其他信息:LINQ to Entities无法识别方法Boolean VerifyHashedPassword(Byte[], System.String)方法,并且此方法无法转换为商店表达式。

错误发生在:

var v = db.AspNetUsers.Single(a => a.UserName.Equals(user.UserName) && VerifyHashedPassword(Convert.FromBase64String(a.PasswordHash), user.PasswordHash) == true);

2 个答案:

答案 0 :(得分:6)

Linq2Entities尝试将您的表达式转换为SQL。像VerifyHashedPassword这样的自定义C#代码无法翻译。

作为解决方法,首先执行Linq查询,然后在内存中应用自定义代码。

var matchingUsers = db.AspNetUsers.Single(a => a.UserName).ToArray(); // enumerating the query will execute it

var v = matchingUsers.SingleOrDefault(a => VerifyHashedPassword(Convert.FromBase64String(a.PasswordHash), user.PasswordHash) == true);

答案 1 :(得分:0)

那是因为你在这里用表达式写的东西是有限的

var v = db.AspNetUsers.Single(a => a.UserName.Equals(user.UserName) && VerifyHashedPassword(Convert.FromBase64String(a.PasswordHash), user.PasswordHash) == true);

另外,在你的情况下更好地使用FirstOrDefault并将bool值与true进行比较并不是很有意义。 我建议将代码重写为


public ActionResult Login(AspNetUser user)
        {
            using (eMediCareDBEntities db = new eMediCareDBEntities())
            {
                //byte[] temp = Convert.FromBase64String("AEh/kUSTE019aDWRoSscnT0c/XArWnjMyBeIgQ1MgTAqRetiD84KgdkFAgHO/bfFKQ==");

                //bool result = VerifyHashedPassword(temp, user.PasswordHash);

                var v = db.AspNetUsers.FirstOrDefault(a => a.UserName == user.UserName);
                if(v != null &&  v.VerifyHashedPassword(Convert.FromBase64String(a.PasswordHash), user.PasswordHash))
                {
                    Session["Id"] = v.Id.ToString();
                    Session["UserName"] = v.UserName.ToString();
                    return RedirectToAction("Main");
                }
                else
                {
                    ModelState.AddModelError("", "Username or Password is wrong.");
                }
            }
            return View();
        }