登录问题

时间:2014-01-31 09:19:53

标签: c# asp.net-mvc asp.net-mvc-4

挣扎新手:(

实体未提取用户..

登录控制器

[HttpPost]
public ActionResult LoginForm(string user_NA, string user_pwd)
{
    User u = new User();
    u.LogIn(user_NA, user_pwd);
    return RedirectToAction("Index");
}

登录模式

public bool LogIn(string userID, string password)
{
    using (Entity.xcLEntities en = new xcLEntities())
    {
        try
        {
            var user = en.Users.SingleOrDefault(x => x.LoginID == userID && x.PasswordHash == this.SetPassword(password));
            if (user != null && password != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

    }
}

例外情况

An exception of type 'System.NotSupportedException' occurred in AML.Web.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String SetPassword(System.String)' method, and this method cannot be translated into a store expression.

2 个答案:

答案 0 :(得分:3)

关于您的异常,您可以尝试在SingleOrDefault方法之外获取相应的哈希:

public bool LogIn(string userID, string password)
{
    using (Entity.AMLEntities en = new AMLEntities())
    {
        try
        {
            string hashed = this.SetPassword(password);
            var user = en.Users.SingleOrDefault(x => x.LoginID == userID && x.PasswordHash == hashed);
            if (user != null && password != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

    }
}

编辑:正如Rezoan在评论中所建议的那样,通过阅读Entity Framework can't run your C# code as part of its query可以找到更多信息。

答案 1 :(得分:0)

您应该将SetPassword方法的结果存储在本地变量中,并使用lambda表达式中的varoable。例外情况清楚地表明setpassword方法在db端不可用。