简体   繁体   中英

Log in process using entity framework asp.net mvc

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.

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.

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

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