2

在 asp.net mvc 中,scott hanselman 示例显示了如何显示本地环境的迷你分析器

protected void Application_BeginRequest()
        {
            if (Request.IsLocal) { MiniProfiler.Start(); } //or any number of other checks, up to you 
        }

但是,我想更进一步,能够远程查看它,仅适用于特定的登录用户或 ips。

知道怎么做吗?

更新:我使用了以下代码:

protected void Application_EndRequest()
        {
            MiniProfiler.Stop(); //stop as early as you can, even earlier with MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
        }

        protected void Application_PostAuthorizeRequest(object sender, EventArgs e)
        {
            if (!IsAuthorizedUserForMiniProfiler(this.Context))
            {
                MiniProfiler.Stop(discardResults: true);
            }
        }

        private bool IsAuthorizedUserForMiniProfiler(HttpContext context)
        {
            if (context.User.Identity.Name.Equals("levalencia"))
                return true;
            else
                return context.User.IsInRole("Admin");
        }
4

1 回答 1

8

PostAuthorizeRequest如果当前用户不在给定角色或请求来自特定 IP 或您想要的任何检查,您可以订阅事件并丢弃结果:

protected void Application_BeginRequest()
{
    MiniProfiler.Start();  
}

protected void Application_PostAuthorizeRequest(object sender, EventArgs e)
{
    if (!DoTheCheckHere(this.Context))
    {
        MiniProfiler.Stop(discardResults: true);
    }
}

private bool DoTheCheckHere(HttpContext context)
{
    // do your checks here
    return context.User.IsInRole("Admin");
}
于 2013-04-09T08:27:37.707 回答