Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 462
Mir Gulam Sarwar Avatar asked Sep 01 '13 07:09

Mir Gulam Sarwar


People also ask

How can use Session Management in ASP NET MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same. But like session variable these elements cannot preserve values for all requests; value persistence varies depending the flow of request.

What is authentication in ASP NET MVC?

Authentication. Authentication of user means verifying the identity of the user. This is really important. You might need to present your application only to the authenticated users for obvious reasons. Let's create a new ASP.Net MVC application.


1 Answers

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.

like image 120
keeehlan Avatar answered Oct 07 '22 22:10

keeehlan