【问题标题】:Retrieve the Session in the GlimpseSecurityPolicy RuntimeEvent.ExecuteResource在 GlimpseSecurityPolicy RuntimeEvent.ExecuteResource 中检索 Session
【发布时间】:2015-02-20 18:53:29
【问题描述】:

在使用RuntimeEvent.ExecuteResource 时,我可以通过 glimpse 访问会话信息。如果没有这个,axd 文件就会暴露出来,除非特定用户登录,否则我宁愿禁用它。在下面的两个示例中,会话都将为空。我也试过让这个类实现IRequiresSessionState,但这也没有帮助。

namespace Glimpse
{
    public class GlimpseSecurityPolicy:IRuntimePolicy
    {
        public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
        {
            try
            {
                var name = HttpContext.Current.Session["username"];
                var name2 = policyContext.GetHttpContext().Session["username"];
            }
            catch (Exception)
            {
            }

            // You can perform a check like the one below to control Glimpse's permissions within your application.
            // More information about RuntimePolicies can be found at http://getglimpse.com/Help/Custom-Runtime-Policy
            // var httpContext = policyContext.GetHttpContext();
            // if (!httpContext.User.IsInRole("Administrator"))
            // {
            //     return RuntimePolicy.Off;
            // }

            return RuntimePolicy.On;
        }

        public RuntimeEvent ExecuteOn
        {
            // The RuntimeEvent.ExecuteResource is only needed in case you create a security policy
            // Have a look at http://blog.getglimpse.com/2013/12/09/protect-glimpse-axd-with-your-custom-runtime-policy/ for more details
            get { return RuntimeEvent.EndRequest | RuntimeEvent.ExecuteResource; }
        }
    }
}

【问题讨论】:

  • 您是否尝试仅为特定用户启用 Glimpse?
  • 我可以通过在 web.config 中配置 Glimpse 仅允许某些用户使用。 位置>
  • 你必须在 glimpse 上禁用 localpolicy

标签: glimpse


【解决方案1】:

原因是处理 Glimpse.axd 请求的 Glimpse HttpHandler 没有实现 IRequireSessionState 接口。

HttpHandler 最终将执行所有将RuntimeEvent.ExecuteResource 配置为ExecuteOn 属性值的一部分的IRuntimePolicy 实例。

我认为最简单的解决方案是创建自己的IHttpHandler,实现IRequireSessionState 接口并将所有调用转发到Glimpse HttpHandler,如下所示。

public class SessionAwareGlimpseHttpHandler : IHttpHandler, IRequiresSessionState
{
    private readonly HttpHandler _glimpseHttpHandler = 
        new Glimpse.AspNet.HttpHandler();

    public void ProcessRequest(HttpContext context)
    {
        _glimpseHttpHandler.ProcessRequest(context);
    }

    public bool IsReusable
    {
        get { return _glimpseHttpHandler.IsReusable; }
    }
}

不要忘记更新您的 web.config 以使用该处理程序而不是原始处理程序:

...
<system.webServer>
    ...
    <handlers>
        <add name="Glimpse" path="glimpse.axd" verb="GET" type="YourNamespace.SessionAwareGlimpseHttpHandler, YourAssembly" preCondition="integratedMode" />
    </handlers>
    ...
</system.webServer>
...

一切就绪后,您应该可以访问您的IRuntimePolicy 中的Session

【讨论】:

  • 这正是我想要的。解决问题的好方法!
  • 谢谢!太糟糕了 Glimpse 文档没有注意到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2022-01-18
  • 2012-06-16
  • 2023-04-03
相关资源
最近更新 更多