Click here to Skip to main content
15,924,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have created a Registration page and login page, registration page details are saving in DB but login details are not saving in DB. Please let me know How to save the login details in DB like(column Names: LoginID UserName Password.
below is my code.

Thanks in Advance.

What I have tried:

My Model class:
C#
 public class LoginViewModel
    {
        [Key]
        public int LoginID { get; set; }
        [Display(Name = "User Name")]
        public string UserName { get; set; }
        [Display(Name = "Password")]
        public string Password { get; set; }

//Below My ActionResult Method ---

//GET: Login Page
        public ActionResult Login()
        {
            return View();
        }
        //POST: Login Page
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(string UserName, string Password)
        {
            try
            {
                using (var DbContext = new HrmDbContext())
                {
                    var getUser = (from s in DbContext.userregisterviewmodels
                                   where s.UserName == UserName || s.EmailID == UserName
                                   select s).FirstOrDefault();
                    if (getUser != null)
                    {
                        var hashCode = getUser.VCode;
                        //Password Hasing Process Call Helper Class Method    
                        var encodingPasswordString = Helper.EncodePassword(Password, hashCode);
                        //Check Login Detail User Name Or Password
                        var query = (from s in DbContext.userregisterviewmodels
                                     where (s.UserName == UserName || s.EmailID == UserName) && s.Password.Equals(encodingPasswordString)
                                     select s).FirstOrDefault();
                        if (query != null)
                        {
                            //RedirectToAction("Details/" + id.ToString(), "FullTimeEmployees");    
                            //return View("../Admin/Registration"); url not change in browser    
                            return RedirectToAction("AfterLoggedIn", "Home");
                            
                        }
                        ViewBag.ErrorMessage = "Invallid User Name or Password";
                        return View();
                    }
                    ViewBag.ErrorMessage = "Invallid User Name or Password";
                    return View();
                }
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = " Error!!! contact abc@info.in";
                return View();
            }
        }



below is my DbContext--
C#
 public class HrmDbContext : DbContext
    {
public DbSet<userregisterviewmodel> userregisterviewmodels { get; set; }

        public DbSet<loginviewmodel> loginviewmodels { get; set; }
}
Posted
Updated 2-Apr-16 3:40am
v2

1 solution

First, you don't explain what you mean by "saving login details". What are "login details" and why are you saving them?

Next, you have show ZERO code that saves anything to the database.

Looking at your DbContext, you have your tables named as "viewmodels". What is saved in the database is NOT a view model. You've named your object incorrectly and don't in in a fashion that just leads to massive confusion in your code.

View Model classes are used to move data between a Controller and the View it is interacting with. They are NEVER saved to the database.

Now, having said that, your Login method retrieves a userregisterviewmodel form the database that matches the username entered by the user. All you do with it is check to see if the user exists. After that you get the hash value for the entered password and then go back tot he database for a second query for the user object using the password hash value as part of the WHERE clause. YOU DO NOT NEED TO DO THAT!! You already have the user object from the previous query. All you have to do is compare the hash value to the one you already have. You don't have to go back to the database!

Nowhere in this code so you even attempt to save anything to the database as far as "login details".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900