7

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.

Mir Gulam Sarwar
  • 2,588
  • 2
  • 25
  • 39

4 Answers4

12

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.

keeehlan
  • 7,874
  • 16
  • 56
  • 104
0

It should be modified like this...

public ActionResult VerifyPassword(User user)
        {
            //The ".FirstOrDefault()" method will return either the first matched
            //result or null
            User myUser = dbContext.Users.FirstOrDefault
                (u => u.Username.Equals(user.Username) && u.Password.Equals(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
            }
        }
Thilina H
  • 5,754
  • 6
  • 26
  • 56
  • you should compare with with retrieved user object weather it is null or not. So use myUser instead of using user in if clause – Thilina H Sep 01 '13 at 18:15
0
    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();
        }
    }
0
public ActionResult Login(Login_T l)
    {
        using (AppHREntities db = new AppHREntities())
        {
            var obj = db.Login_T.Where(a => a.UserName.Equals(l.UserName) && a.Password.Equals(l.Password) && l.Type.Equals("HR")).Count();
            var obj1 = db.Login_T.Where(a => a.UserName.Equals(l.UserName) && a.Password.Equals(l.Password) && l.Type.Equals("USER")).Count();
            if (obj > 0)
            {
                MessageBox.Show("SUCESSFULLY LOGGED IN.");
                return RedirectToAction("../Home/HR_Dashboard");
            }
            else if(obj1 > 0)
            {
                MessageBox.Show("SUCESSFULLY LOGGED IN.");
                return RedirectToAction("../Home/Emp_Dashboard");
            }
            else
            {
                MessageBox.Show("WRONG PASSWORD.");
                return RedirectToAction("Index");
            }
        }
    }
vishnu
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 05:42