Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would derive from <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.108).aspx" rel="nofollow noreferrer">AuthorizeAttribute</a>. No need to check this information if you don't need to authorize the request.</p> <pre><code>public class SingleLoginAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { bool isAuthorized = base.AuthorizeCore(httpContext); if (isAuthorized) { int userId = (int)WebSecurity.CurrentUserId; if ((filterContext.HttpContext.Session.SessionID != dba.getSessionId(userId)) || dba.getSessionId(userId) == null) { WebSecurity.Logout(); isAuthorized = false; filterContext.Result = new RedirectResult("/Home/Index"); } } return isAuthorized; } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new JsonResult() { Data = FormsAuthentication.LoginUrl, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { base.HandleUnauthorizedRequest(filterContext); } } } </code></pre> <p>I'd also mention that this allows you to short circuit other ActionFilters because they run after OnAuthorization.</p> <ol> <li>Forward Order - OnAuthorization : AuthorizationFilter (Scope Controller)</li> <li>Forward Order - OnActionExecuting : ActionFilter1 (Scope Global)</li> <li>Forward Order - OnActionExecuting : ActionFilter2 (Scope Controller)</li> <li>Forward Order - OnActionExecuting : ActionFilter3 (Scope Action)</li> </ol> <p>Then as <a href="https://stackoverflow.com/users/1002908/rob-lyndon">Rob Lyndon</a> mentioned, you could in the FilterConfig (MVC4)</p> <pre><code>public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new SingleLoginAuthorizeAttribute()); } } </code></pre> <p>Then when you don't want to require any authorization, you can use the <a href="http://blogs.msdn.com/b/rickandy/archive/2012/03/23/securing-your-asp-net-mvc-4-app-and-the-new-allowanonymous-attribute.aspx" rel="nofollow noreferrer">AllowAnonymouseAttribute</a> on your ActionResult methods or Controller Class to allow anonymous access.</p> <p><strong>Update</strong></p> <p>I added a way for your ajax calls (Get or Post) to work with timeouts. You can do something like:</p> <pre><code>success: function (jsonResult) { if (jsonResult.indexOf('http') == 0) { window.location = jsonResult; } // do other stuff with the Ajax Result } </code></pre> <p>This isn't exactly the best way, but if you want more information on how to do this better I would ask another question instead of appending more questions on this one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload