When using ServiceStack authentication with ASP.NET MVC, I wanted to implement a sliding session expiration. After some help from @mythz, I got it working. For any who want to do the same, see my answer for my final implementation.
Here's my final implementation for a sliding session when using ServiceStack authentication in ASP.NET MVC...
public class SlideSessionExpirationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ssController = filterContext.Controller as ServiceStackController;
if (ssController == null) return;
// Get session and re-save to slide/update the expiration date
var session = ssController.ServiceStackRequest.GetSession();
if(session != null && session.IsAuthenticated)
ssController.ServiceStackRequest.SaveSession(session, AppHost.SessionExpiration);
base.OnActionExecuting(filterContext);
}
}
...where AppHost.SessionExpiration
is a static readonly TimeSpan
that I declared in AppHost.cs
. To use it, you can slap the attribute on a controller or method via [SlideSessionExpiration]
or you can add it in via a global filter (like I did) inside FilterConfig.cs
via...
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
/* ... other filters ... */
filters.Add(new SlideSessionExpirationAttribute());
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments