asp.net - 如何使用 AspNetSqlMembershipProvider 正确验证 mvc-mini-profiler

标签 asp.net asp.net-mvc-3 mvc-mini-profiler

我尝试使用此代码检查用户是否在 Application_BeginRequest 和 Application_AuthenticateRequest 中起作用,但它不起作用。在 BeginRequest 时,代码永远不会被命中,并且 Authenticate 它被某些请求命中,并且分析器不会出现。

仅检查 Request.IsLocal 工作正常。

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

任何想法或为什么它不起作用或更好的方法来做到这一点?

[更新] 我接受了遮阳篷,但因为我不太明白它的工作原理而取消了它

我做了以下操作,但分析器一开始没有出现。
几次尝试后,它开始出现,即使我尝试以隐身模式访问该网站,也没有 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 拥有。

最佳答案

cookie feanz 提到的是一个方便的技巧,第二种方法是无条件分析,然后为未经身份验证的用户放弃 session :

protected void Application_BeginRequest()
{
   MvcMiniProfiler.MiniProfiler.Start();  
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
  if(!CurrentUserIsAllowedToSeeProfiler())
  {
    MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
  }
}

关于asp.net - 如何使用 AspNetSqlMembershipProvider 正确验证 mvc-mini-profiler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6349280/

相关文章:

c# - MiniProfiler 没有出现在 ASP.NET Core 中

asp.net - 无法在 Visual Studio 2015 中打开“发布”对话框(ASP.NET 5 Web 应用程序)

c# - 如何在文本框中键入内容时触发文本框 textchanged 事件

Asp.Net VNext Windows 服务

asp.net-mvc - 具有 HTML 5 数据属性的 ActionLink 的 MVC3 奇怪行为

ajax - 如何在mvc3中通过ajax调用局部 View

asp.net-mvc-3 - 使用 MVC Razor 在 if/else 中混合文本和函数

asp.net - Webmatrix - WebGrid : change css style conditionally

c# - MiniProfiler 结果 UI 显示空名称,因为使用了 SqlServerStorage

asp.net - 如何正确解释 SqlServerStorage 记录的 MiniProfiler 计时?