Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My MVC5 project structure goes like this:::

Project
  - App_Data
  - App_Start
  - Controllers
      - AccountController.cs
      - HomeController.cs
  - Views
      - Account
          - Index.cshtml
      - Home
          - Index.cshtml
      - web.config
  - Global.asax
  - Web.config


In View -> Account -> Index.cshtml is login page whose view for now is:
<body>
    <div>
        @using (Html.BeginForm("Login", "Account", FormMethod.Post))
        {
          <p>
              from Account
          </p>
          <input type="submit" value="Post" />
        }
        
    </div>
</body>


AccountController code is:

public class AccountController : Controller
  {
      // GET: Account
      public ActionResult Index()
      {
          return View();
      }

      [HttpPost]
      [AllowAnonymous]
      public ActionResult Login()
      {
          if (true)  //assuming validation is true
          {
              FormsAuthentication.RedirectFromLoginPage("test", false);
          }
          return View();
      }
  }




Strangely, when I click on button Post(), which is under form POST, it is hitting breakpoint in controller in Index() method instead of Login().

Web.config file for formsauthentication is:::

<system.web>

   <authentication mode="Forms">
     <forms loginUrl="~/Account/Index" defaultUrl="~/Home/Index" >
       <credentials passwordFormat="Clear"></credentials>
     </forms>
   </authentication>
   <authorization>
     <deny users="?"/>
   </authorization>

   <compilation debug="true" targetFramework="4.6.1"/>
   <httpRuntime targetFramework="4.6.1"/>
 </system.web>


I am not understanding the behaviour of MVC now. Why is this happening???

What I have tried:

If I do not decorate [AllowAnonymous], no post will happen. I removed forms authentication and it worked fine.
Posted
Updated 13-Mar-19 8:17am
Comments
F-ES Sitecore 13-Mar-19 13:54pm    
Are you sure it isn't doing the Post action and then the Index action? The code in Post is redirecting to the Index action so it will always go to Index after Post.

1 solution

Yes, it is not going to post but going to Index action. Debugger was set in all action method but code did not go to Login(). For double crosschecking, I did below code and verified what I said earlier.

public class AccountController : Controller
   {
       // GET: Account
       public ActionResult Index()
       {
           return View();
       }

       [HttpPost]
       [AllowAnonymous]
       public ActionResult Login()
       {
           if (true)  //assuming validation is true
           {
               FormsAuthentication.RedirectFromLoginPage("test", false);
           }
           return null;
       }
 
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