When working with ASP.NET MVC and WebMatrix.WebData.SimpleMembershipProvider, I faced an issue of ASP.NET redirecting to incorrect login page i.e. “/login.aspx” in case of unauthorized access. As a result, got server error something like below snapshot
The resolution is simple and somewhat intuitive. Somebody needs to tell ASP.NET my LoginUrl. There are two workaround for that which I have tried and worked for me.
- Add a LoginUrl key in appSettings in web.config
<add key="LoginUrl" value="~/Account/Login"/>
- OR Set the form loginUrl in
<forms loginUrl="~/Account/Login" timeout="3000" />
Another way around I found on stackoverflow is to set FormAuthenticationSettings but I didn’t give it a shot.
Finally, if you faced issue of redirect to ~/Account/Logon instead of ~/Account/Login in MVC 4 then make sure to check the PreserveLoginUrl is added and set true in your appSettings as mentioned in ASP.NET MVC 4 release notes
Hope it helps.