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.

3
  • What are you trying to do? Is this for changing a user's password? Commented Sep 1, 2013 at 7:14
  • no.Password verification,before log in Commented Sep 1, 2013 at 7:20
  • 1
    You should probably just use the built-in membership system, since it's almost guaranteed you will do this in an insecure manner (even people who should know better typically do it insecurely). Commented Sep 1, 2013 at 7:29

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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
            }
        }

1 Comment

you should compare with with retrieved user object weather it is null or not. So use myUser instead of using user in if clause
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();
        }
    }

Comments

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");
            }
        }
    }

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.