2013-04-09 72 views
2

在asp.net MVC斯科特Hanselman的示例演示如何显示迷你探查了本地environmnet使迷你探查只有特定的用户和角色

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

回答

6

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

我怎样才能从在Global.asax当前用户? – 2013-04-09 08:31:22

+0

用户不是Request中的类吗?我错过了一个程序集引用或我想用的吗? – 2013-04-09 08:33:37

+0

我看到这个:context.Request.LogonUserIdentity.User,也许这个对用户有效,但是当我尝试使用这个 – 2013-04-09 08:35:17

相关问题