【问题标题】:How to restrict access to /mini-profiler-resources/results-index per request如何限制每个请求对 /mini-profiler-resources/results-index 的访问
【发布时间】:2015-12-08 22:33:16
【问题描述】:

限制对 mini-profiler 资源访问的示例都发生在 Application_Start 方法中,这令人困惑,因为这将根据第一个访问网站。

稍后在示例中,他们展示了如何根据每个请求放弃 profiler 信息,该请求在全局范围内启用它然后拒绝每个请求,但这在 /results-index 页面上不起作用。

有没有办法只允许每个请求访问/results-index 页面,或者以类似的方式放弃这个info/page

【问题讨论】:

  • 没有意识到它只是指派代表稍后调用,我使用匿名代表进行测试,这不应该有什么不同,但我相信我现在可以工作了

标签: asp.net-mvc mvc-mini-profiler


【解决方案1】:

我是如何做到的:
让所有控制器都继承自一个共同的 BaseController 类。
BaseController 中,覆盖Initialize

protected override void Initialize(RequestContext requestContext)
{
    if (requestContext.HttpContext.User == null || !requestContext.HttpContext.User.IsInRole(KnownRoles.Developer.ToString()))
    {
        MiniProfiler.Stop(discardResults: true);
    }
    base.Initialize(requestContext);
}

编辑:您可以在 web.config 中限制对 miniprofiler 历史页面的访问:

  <location path="mini-profiler-resources">
    <system.web>
      <authorization>
        <allow roles="Developer"/>
        <deny users="*" />
      </authorization>
    </system.web>
  </location>

这将只允许具有“开发者”角色的用户访问该页面。

【讨论】:

  • 这将放弃页面中显示的小部件,但不会影响“最后 100 个已配置”项目页面 /results-index
  • 用索引页面的解决方案编辑了我的答案。如果您想根据角色或个人用户限制访问,这应该可以工作。
  • 感谢您的更新。当我对此进行测试时,我使用了一个不带参数的匿名委托,我相信这会导致它在 app_onstart 中只运行一次。当我切换到与示例页面中的签名匹配的委托时,它按预期工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多