14

このコードを使用して、ユーザーが Application_BeginRequest および Application_AuthenticateRequest で役割を果たしているかどうかを確認しようとしましたが、機能しません。BeginRequest では、コードはヒットせず、Authenticate では一部のリクエストでヒットし、プロファイラーは表示されません。

Request.IsLocal のみをチェックすると正常に動作します。

if(Request.IsAuthenticated)
{
  if(User.IsInRole("Admin");
    MiniProfiler.Start(); 
}

アイデアや、なぜそれがうまくいかないのか、それを行うためのより良い方法はありますか?

[更新]私は awnser を受け入れましたが、うまく動かなかったので元に戻しました

次のことを行いましたが、最初はプロファイラーが表示されません。数回試行した後、シークレットモードでサイトにアクセスしようとしても表示されるようになったため、Cookie はありませんでした。

protected void Application_PostAuthorizeRequest(Object sender, EventArgs e)
{
        if (User.IsInRole("Admin"))
        {
            HttpCookie cookie =   HttpContext.Current.Request.Cookies.Get("RoleProfiler");
            if (cookie == null)
            {
                cookie = new HttpCookie("RoleProfiler");
                cookie.Value = "yes";
                cookie.Expires = DateTime.Now.AddDays(1d);
                Response.Cookies.Add(cookie);
            }
        }
 }

そして、私はチェックしています

protected void Application_BeginRequest(Object sender, EventArgs e)
{            
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("RoleProfiler");
        if ((cookie != null) && (cookie.Value == "yes") )
        {
            MvcMiniProfiler.MiniProfiler.Start();
        }
 }

そして、リクエストの最後で終了します。

protected void Application_EndRequest()
{
        MvcMiniProfiler.MiniProfiler.Stop();
}

[更新 2]締めくくりの質問です。これは無視してください。私は outputcache に所有されていました。

4

3 に答える 3

8

開始リクエストは、リクエスト ライフ サイクルでユーザーが完全に認証される前に発生します。

リクエストが認証されたときにユーザーがロール(あなたの場合は「管理者」)にいる場合はCookieを追加することでこの問題を解決し、開始リクエストでこのCookieを確認してプロファイラーを初期化できます。

初めては機能しませんが、その後は毎回機能するはずです。

于 2011-06-14T20:01:46.923 に答える
5

これは私の2セントです。

        context.AcquireRequestState += (sender, e) =>
        {
            // Check debug in session. Can be set from Querystring. (?debug=true)
            if (HttpContext.Current.Session != null && HttpContext.Current.Session["Debug"] != null)
            {
                try{
                    bool debug = (bool)HttpContext.Current.Session["Debug"];
                    if (debug == true) 
                        MiniProfiler.Start();
                    else 
                        MiniProfiler.Stop(discardResults: true);
                }
                catch{ 
                    MiniProfiler.Stop(discardResults: true);
                }

            }// Or always show if Administrator.
            else if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                bool admin = HttpContext.Current.User.IsInRole("Administrator");
                if (admin == false)
                {
                    MiniProfiler.Stop(discardResults: true);
                }
            }
            else
            {
                MiniProfiler.Stop(discardResults: true);
            }
        };
于 2012-12-11T10:59:00.050 に答える