0

This my Login Method in Repository Class Where i'm writing a logic to check the email id and password that will matches already registered user in the database and returns true if the email and password matched in db

    public bool Login(Models.Login user)
    {
        Eseal.UserRegister u = null;
        try
        {
            string Dpassword = Decrypt(user.Password);
            using (var dbContext = new MVCDEMOEntities())
            {
                u = dbContext.UserRegisters.Where(query => query.EmailID.Equals(user.EmailID) && query.Password.Equals(user.Password)).SingleOrDefault();
            }
            if (u == null)
                return false
            else
                return true;
        }
        catch (Exception)
        {

            throw;
        }
     }

This my Account Controller code

public ActionResult Login(Models.Login user)
    {
       try
        {
            var services = new RegisterService();
            services.LoginRegister(user);

            return RedirectToAction("Privacy");
        }
        catch (Exception)
        {

            throw;
        }

    }

This is my save register method

  public string SaveRegister(Models.UserRegister model)
    {
        try
        {
            using (var dbcontext = new MVCDEMOEntities())
            {
                var dbRegister = new Eseal.UserRegister()
                {
                    Id = Guid.NewGuid(),
                    Password = encrypt(model.Password),
                    EmailID = model.EmailID,
                    ContactNumber = model.ContactNumber,
                    CreatedOn = DateTime.Now,
                    CreatedBy = 1,
                    UpdatedOn=null,
                    UpdatedBy=null,
                    IsExists= true
                 };

                dbcontext.UserRegisters.Add(dbRegister);
                dbcontext.SaveChanges();

            }
        }

This is my Service Class

public void LoginRegister(Models.Login user)
    {
        var result = _repository.Login(user);
    }
Niranjan S
  • 132
  • 9

1 Answers1

2

You compare encrypted password in database against plain text password provided by user.

You should encrypt plain password before comparing it with password in database.

public bool Login(Models.Login user)
{
    var encryptedGivenPassword = encrypt(user.Password);
    using (var dbContext = new MVCDEMOEntities())
    {
        return dbContext.UserRegisters.Where(u => u.EmailID == user.EmailID)
                                      .Where(u => u.Password == encryptedGivenPassword)
                                      .Any();
    }
 }

Your service method should return result of Login method

public bool LoginRegister(Models.Login user) 
{ 
    return _repository.Login(user); 
}

Then you call this method in the login controller

public ActionResult Login(Models.Login user)
{
   var services = new RegisterService();
   if (services.Login(user))
   {
       return RedirectToAction("Privacy");
   }
   else
   {
       return Unathorized();
   }
}

You can get rid of you redundant try .. catch wrappers.

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • the code you modified also not working. it's working if the user is not registered also. – Niranjan S Aug 13 '18 at 06:41
  • @NiranjanS, You need to call `Login` method in the controller. I afraid that when you call `LoginRegister` you will register new user instead of just validate it. – Fabio Aug 13 '18 at 06:55
  • public void LoginRegister(Models.Login user) { var result = _repository.Login(user); } I am calling Login Method in Service layer – Niranjan S Aug 13 '18 at 06:56
  • @NiranjanS, you need return result of `Login` method back to controller, that you can return correct View based on that result. – Fabio Aug 13 '18 at 07:01
  • Can You Please @Fabio send me any reference links of code or please tell me where to modified my code. because i am new to MVC – Niranjan S Aug 13 '18 at 07:09