Details
I'm using ASP.NET MVC with Angular. If I do a hard refresh (F5), it hits the attribute I created just fine.. also when the session cookies exist it accesses it fine. but say the user is on a certain page, and the session cookie expires while he's on it.. the code will still access my Angular code, but once it's supposed to hit my Controller or probably my Attribute first.. it doesn't. So nothing works on the webpage at that point and nor does it redirect to the Login screen.
I googled around and searched this website as well but haven't found anything that works.. Any help?
My Code
Attribute for all my Controllers EXCEPT by AccountController (it causes a Redirect loop for some reason??). I put this at the top of all my controllers.
[CustomFilters.VerifySession]
My Custom Attribute
public class CustomFilters
{
public class VerifySessionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var userId = filterContext.HttpContext.Session["UserId"];
var userName = filterContext.HttpContext.Session["UserName"];
if (userId == null || userName == null)
filterContext.Result = new RedirectResult(string.Format("/Account/Login"));
}
}
}
My Login Function
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model)
{
...
Session["UserId"] = UserId;
Session["UserName"] = sql.UserProfiles.Where(c => c.UserId == UserId).Select(x => x.UserName).FirstOrDefault();
...
}
Web.config
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="1" /> //set to 1 just for testing purposes
</authentication>
<sessionState timeout="1" /> //set to 1 just for testing purposes
</system.web>