【问题标题】:How to properly authenticate mvc-mini-profiler with AspNetSqlMembershipProvider如何使用 AspNetSqlMembershipProvider 正确验证 mvc-mini-profiler
【发布时间】:2011-06-14 19:54:45
【问题描述】:

我尝试使用此代码检查用户是否在 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();
}

[Update2]结束问题,忽略这个,我被 outputcache 所有。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 如果您使用角色来确定访问权限,您需要在Application_PostAuthorizeRequest (msdn.microsoft.com/en-us/library/…) 中进行检查。角色模块在AuthenticateRequest 完成之前不会触发,因此User.IsInRole("Profiler") 将始终在Application_AuthenticateRequest 中返回false
    • @Adam ...很好,您可以在请求生命周期中的任何时候中止分析结果...同样为了额外的低开销,您只能在存在 cookie 时开始分析,然后加倍如果需要,检查并放弃
    【解决方案2】:

    开始请求发生在用户在请求生命周期中完全通过身份验证之前。

    我通过添加 cookie 解决了这个问题,如果用户在请求通过身份验证时处于角色(在您的情况下为“管理员”),那么您可以在开始请求时检查此 cookie 并初始化分析器。

    第一次不行,以后每次都可以。

    【讨论】:

      【解决方案3】:

      这是我的 2cent。

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

      【讨论】:

      • 也许您可以在代码中添加一点解释,以使答案更有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多