仅为特定用户和角色启用迷你探查器

时间:2013-04-09 08:24:10

标签: c# asp.net-mvc-4 mvc-mini-profiler

在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");
        }

1 个答案:

答案 0 :(得分:7)

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

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");
}