【问题标题】:Access session in HttpModule in or after Response.Filter?在 Response.Filter 中或之后访问 HttpModule 中的会话?
【发布时间】:2016-06-25 22:16:45
【问题描述】:

我正在使用 Azure 会话状态提供程序 (DistributedCacheSessionStateStoreProvider),效果很好。但现在我有一个自定义的 HttpModule 组合脚本。我在PostAcquireRequestState 中连接Response.Filter。然后我通过构造函数发送Session 对我的自定义过滤器的引用:

application.Response.Filter = new CombinationFilter(application.Response.Filter, application, application.Session)

在我的过滤器中修改Session,它可以在本地主机(标准会话提供程序)上完美运行。但是当我修改 Session 时在 Azure 上发布了这些值,但后来它们消失了(它们没有被持久化)。只有添加到过滤器中的那些会消失,之前存在的那些仍然存在。我怀疑最后一次同步在ReleaseRequestState 内(基于名称)。这显然是在我的Response.Filter 被处理之前。

  1. 如何在链中稍后访问Session 并仍然存储它?

  2. 或者我可以在ReleaseRequestState之前使用过滤器吗?

【问题讨论】:

    标签: asp.net session azure iis httpmodule


    【解决方案1】:

    我终于解决了。对于其他可能想知道如何实现这一点的人:

    application.PostRequestHandlerExecute += (sender, args) =>
    {
        if (YourCondition)
        {
            // performs premature flush to force the filter
            application.Response.Flush();
            String content = ((CombinationFilter) application.Response.Filter).OriginalContent;
    
            // we need to remove the filter to not execute twice
            application.Response.Filter = null;
    
            // do my stuff here, we still have a Session and also the original content
            // the content is stored on a filter in Flush() method before modification 
            // (by me => my custom property)
    
            // do not write any other content
            application.Response.SuppressContent = true;
    
            // onwards it still saves Session in ReleaseRequestState (presumably)
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2013-09-05
      • 2011-06-23
      • 2011-02-03
      • 1970-01-01
      相关资源
      最近更新 更多