如何使用AspNetSqlMembershipProvider正确认证mvc-mini-profiler

问题描述

| 我尝试使用此代码检查用户是否在Application_BeginRequest和Application_AuthenticateRequest中扮演角色,并且该代码不起作用。在BeginRequest上,代码永远不会被命中,并且对某些请求的身份进行身份验证,并且探查器不会显示。 仅检查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();
}
[Update2]关闭问题,请忽略此问题,我由outputcache拥有。     

解决方法

        开始请求发生在请求生命周期中对用户进行完全认证之前。 如果用户通过身份验证(如果您在角色中为\“ Admin \”),则通过添加cookie来解决此问题,然后您可以在开始请求时检查此cookie并初始化分析器。 它不会第一次工作,但之后应该每次工作。     ,        cookie feanz提到的这是一个方便的技巧,第二种方法是无条件地进行概要分析,然后为未经身份验证的用户放弃会话:
protected void Application_BeginRequest()
{
   MvcMiniProfiler.MiniProfiler.Start();  
}
protected void Application_AuthenticateRequest(Object sender,EventArgs e)
{
  if(!CurrentUserIsAllowedToSeeProfiler())
  {
    MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
  }
}
    ,        这是我的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);
            }
        };