2013-04-09 70 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

相關問題