简体   繁体   中英

Server Error in '/' Application.Specified cast is not valid

在此处输入图片说明 I tried to run this code and error pops out specific cast not valid. Before any LINQ was added is doing fine. Please, help thank you. Before any of the LINQ was added still fine. But now it couldn't load the User database context. I have no idea how this happened. Please send help thank you.

ForumDBDataContext db = new ForumDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
    ValidationSettings.UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
}

protected void btnLogin_Click(object sender, EventArgs e)
{
    string emailAddress = txtEmail.Text;
    string password = txtpwd.Text;
    bool rememberMe = chkRememberMe.Checked;

    User u = db.Users.SingleOrDefault(
                x => x.EmailAddress == emailAddress &&
                x.Hash == Security.GetHash(password)
                );

    if (u != null)
    {
        DateTime currentDate = DateTime.Now;
        var query = from m in db.Members
                    where m.EmailAddress == emailAddress
                    select m.Last_Login_Date;

        Convert.ToDateTime(currentDate);
        DateTime lld = Convert.ToDateTime(query);
        int diffDays = DateTime.Compare(currentDate, lld);


        if (diffDays > 72)
        {
            Response.Redirect("ChangePwd.aspx");
        }
        else
        {
            Security.LoginUser(u.Name, u.Role, rememberMe);

        }
    }

You can not use Security.GetHashmethod in LINQ since this method can not be translated into SQL. You need to run this method and set variable before run LINQ:

string emailAddress = txtEmail.Text;
string password = txtpwd.Text;
bool rememberMe = chkRememberMe.Checked;
var hashedPassword = Security.GetHash(password);

User u = db.Users.SingleOrDefault(
            x => x.EmailAddress == emailAddress &&
            x.Hash == hashedPassword 
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM