只对特定的用户和角色启用迷你分析器

本文关键字:启用 角色 分析器 用户 | 更新日期: 2023-09-27 18:08:17

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

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

但是,我想更进一步,能够远程看到它,只针对特定的登录用户或ip。

知道怎么做吗?

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

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

只对特定的用户和角色启用迷你分析器

您可以订阅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");
}