Log in process using entity framework asp.net mvc

asp.net-mvc c# entity-framework

Question

I want to have log in password verification in my project.when user clicks on the log in button compiler goes to this method

public ActionResult VerifyPassword(User user)
{
    var givenPassword =user.Password;
    var givenUserName=user.UserName;
//now i need compare password 
    var myUser=db.User.Find(somevalue)//find user from database,
    But how can i do this????Because somevalue needs to be a Primary Key

}

If i am doing something wrong.Please point me into right direction I searched a lot on the Web.But no tutorial was found to accomplish this using entity framework.

1
6
9/1/2013 7:29:00 AM

Accepted Answer

You actually don't need a primary key to match a user in your database.

You can use their username (which should be unique) to locate their record in the database.

Try something like this:

public ActionResult VerifyPassword(User user)
{
    //The ".FirstOrDefault()" method will return either the first matched
    //result or null
    var myUser = db.Users
        .FirstOrDefault(u => u.Username == user.Username 
                     && u.Password == user.Password);

    if(myUser != null)    //User was found
    {
        //Proceed with your login process...
    }
    else    //User was not found
    {
        //Do something to let them know that their credentials were not valid
    }
}

Also consider doing a bit of research on Model validation, looking into ModelState.IsValid is a great start.

10
9/2/2013 3:39:52 AM

Popular Answer

    public ActionResult Login(StudentLogin sl)
    {
        if (sl.Email != null)
        {

            if (ModelState.IsValid) // this is check validity
            {
                StudentEntities1 se = new StudentEntities1();
                var v = se.StudentLogins.Where(a => a.Email.Equals(sl.Email) && a.Password.Equals(sl.Password)).FirstOrDefault();

                if (v != null)
                {
                    Session["LogedUserID"] = v.Id.ToString();
                    //Session["LogedUserFullname"] = v.FullName.ToString();
                    return RedirectToAction("Success", "Student");
                }

            }
            return View(sl);
        }
        else
        {
            return View();
        }
    }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow