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