1

I am using Forms Auhtentication in MVC4, my problem is that users are not redirect from login page after authentication.

Below is my Login Action:

[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
   if (ModelState.IsValid)
   {
      if (Membership.ValidateUser(model.UserName, model.Password))
      {
         FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);
         RedirectToDefault(returnUrl,model.UserName);
      }
      else
      {
         this.ShowMessage(MessageType.Danger, "Invalid username or password.");
      }
   }

   this.ShowMessage(MessageType.Danger, "Invalid username or password.");
   return View(model);
}

and my Web.Config :

<authentication mode="Forms">
   <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

This code :

FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);
RedirectToDefault(returnUrl, model.UserName);

are executed but the code still got the last Return View(model);

Please any help will be appreciated.

3
  • You saying its not redirecting you from the auth page to another? Like the index page?? Commented Apr 15, 2014 at 11:10
  • Yes, even when username and password are correct that is Membership.ValidateUser(model.UserName, model.Password) return true. Commented Apr 15, 2014 at 11:17
  • Why not, instead of RedirectToDefault, use RedirectToAction and point it toward your index page? Commented Apr 15, 2014 at 13:36

2 Answers 2

1

These are my assumptions on your issue.

Does your Login is separate view in your project or partial?

  1. Make Sure your Login view is not partial View at Form Post.
  2. Make Sure your Login view is not having any Syntax Error in HTML/Code Errors.
  3. Make sure your view properly included in Solution of a project.
  4. Have you passed correct model in your Action Result?
  5. Do you have a code to specify the LoginModel in your view?

Solution to Identify the Issue:

To Identify the Issue Just Add this in your Global.asax:

protected void Application_Error(object sender, EventArgs e)
{// Keep Debugger Here
     Exception exception = Server.GetLastError(); 
     Response.Clear(); // Just check the exception variable hint in Quick Watch
}
Sign up to request clarification or add additional context in comments.

2 Comments

am suprise if i use thiss code if (model.UserName == "mark" && model.Password == "test") { return RedirectToAction("Index", "Home"); } it worked fine
Yes the Mistake is you Returned the Same view name with Same Model in your ActionResult.
0

It turns out that I have called a PartialView on the home page. The Controller where I have the PatialView Result was decorated with [Authorize] Attribute.

All I did was that I decorated the Partiview Result with [AllowAnonymous] and that solved the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.