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
{
public ActionResult Index()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Login()
{
if (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.