如何限制每个请求对 /mini-profiler-resources/results-index 的访问

How to restrict access to /mini-profiler-resources/results-index per request

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

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

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

我是怎么做到的:
让所有控制器都继承自一个公共 BaseController class.
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>

这将只允许具有 "Developer" 角色的用户访问该页面。