1

im having a hard time trying to solve this issue. when i input a username and a password ive always redirect again in log in page. why this happen???

here's my code in login

account controller:

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                     if (Roles.IsUserInRole("Employer"))
                    {
                        return RedirectToAction("CustomerIndex", "Customer");
                    }
                     else if (Roles.IsUserInRole("Worker"))
                    {
                        return RedirectToAction("WorkerIndex", "Worker");
                    }
                     else if (Roles.IsUserInRole("Administrator"))
                     {
                         return RedirectToAction("ClientIndex", "Client");
                     }
                     else 
                     {
                         return View(model);
                     }

                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

thanks.for those who willing to help :)

Vinz with the Z
  • 157
  • 1
  • 14

1 Answers1

1

Looking at your code there are 3 possible cases when this can happen:

  1. Model validation failed -> for example the user didn't provide a username
  2. The user provided incorrect credentials and the ValidateUser method returned false
  3. Credential validation succeeded but the user is not in any of the roles Employer, Worker or Administrator.

Put a breakpoint in your code to see which is your case and act accordingly.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    i tried to put a break point, at first log in when i login and click f10 in my controller it goes in all the condition. but when i log in at second it goes in the controller and in what role of that account was.mmmm – Vinz with the Z Oct 08 '13 at 09:24