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

Solution: 

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   in web.config
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="3000" />
authentication>
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.

 

2 Comments

Paul Pacurar · January 16, 2014 at 7:51 am

This was very helpful for me and solved the problem! Castle.Windsor was generating a bad route for login. Thanks a lot!

Fredy · January 6, 2015 at 11:54 pm

Thank you! This was very useful for me. Have a great day! keep blogging!

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *