Yes, but it's quite a hack. It also won't work with SignalR because SignalR MUST run before session is acquired to prevent long session locks.
Do this to enable session for any request:
public static class AspNetSessionExtensions
{
public static IAppBuilder RequireAspNetSession(this IAppBuilder app)
{
app.Use((context, next) =>
{
// Depending on the handler the request gets mapped to, session might not be enabled. Force it on.
HttpContextBase httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
httpContext.SetSessionStateBehavior(SessionStateBehavior.Required);
return next();
});
// SetSessionStateBehavior must be called before AcquireState
app.UseStageMarker(PipelineStage.MapHandler);
return app;
}
}
Then you can access the session with either HttpContext.Current.Session
or
HttpContextBase httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…