I have an MVC application using Owin Cookie Authentication. I have SlidingExpiration enabled and working. However, when a user's login expires and they are sent back to the LoginPath, the ReturnUrl is giving me some problems:
- I only want the ReturnUrl to be included if it points to a GET action, not a POST action.
- I would like to include the PathAndQuery instead of just Path so that I can re-fill any items the user might have had filled in on a form.
I tried creating my own AuthorizeAttribute (code below) and applying it to some of the methods in one of my controllers, but it seems like it is never hit when the session is expired.
public class CheckLoginExpirationFilter : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
string returnUrl = null;
if (filterContext.HttpContext.Request.HttpMethod.Equals("GET", StringComparison.CurrentCultureIgnoreCase))
returnUrl = filterContext.HttpContext.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary()
{
{ "client", filterContext.RouteData.Values[ "client" ] },
{ "controller", "Security" },
{ "action", "Login" },
{ "ReturnUrl", returnUrl }
});
}
}
}
An answer to a related question indicates that a custom AuthorizeAttribute is the "standard [solution], when you want to override this behavior," but I can't seem to get it to work.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…