My application (MVC4/C#) uses the SimpleMembershipProvider and generally works fine. However, I have a problem that I cannot resolve after spending many hours researching and testing.
If I leave my application for a period of time (say 30 minutes) then select a menu item, the page renders (sidebar/header/footer), but the @RenderBody section redirects to the ~/Account/Login action.
If I then ignore the login and click on any controller action link (from the menu) then it loads as expected. It appears that the razor layout view correctly thinks I am authenticated, but the controller doesn't think I am authorized. I have a base class for most of my controllers that I inherit from that has the [Authorize] attribute.
If I logout, only the RenderBody section renders as expected, for ~/Account/Login action.
From web.config
<system.web>
<roleManager enabled="true" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Base controller
[Authorize]
public abstract class AuthorizeBaseController : Controller
{
}
Controllers
public class SiteController : AuthorizeBaseController
{
private SiteContext db = new SiteContext();
public ActionResult Index()
{
return View(db.Sites.ToList());
}
:
_Layout.cshtml
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Styles.Render("~/Content/menubar.css")
</head>
<body>
@if (Request.IsAuthenticated)
{
<div id="header">
:
</div>
<div id="sidebar">
:
</div> <!-- sidebar -->
}
<div id="body">
@RenderBody()
</div>
@if (Request.IsAuthenticated)
{
<footer>
:
</footer>
}
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>