我正在使用 MVC 迷你探查器,我只为处于“探查器”角色的经过身份验证的用户显示探查器。MiniProfiler.cs 中的示例使用 AuthenticateRequest 方法来确定它是否应该停止分析,但我切换到使用 PostAuthorizeRequest (在阅读了这个问题之后),以便我可以访问 IPrincipal 和 IsInRole 方法。我可以只在 PostAuthorizeRequest 方法中启动探查器,还是应该继续停止并丢弃 PostAuthorizeRequest 中的结果?为每个请求启动和停止分析器的开销是多少?
当前代码:
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) =>
{
MiniProfiler.Start();
};
context.PostAuthorizeRequest += (sender, e) =>
{
var user = ((HttpApplication)sender).Context.User;
if (user == null || !user.Identity.IsAuthenticated || !user.IsInRole("Profiler"))
{
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
};
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}
建议代码:
public void Init(HttpApplication context)
{
context.PostAuthorizeRequest += (sender, e) =>
{
var user = ((HttpApplication)sender).Context.User;
if (user != null && user.Identity.IsAuthenticated && user.IsInRole("Profiler"))
{
MiniProfiler.Start();
}
};
context.EndRequest += (sender, e) =>
{
MiniProfiler.Stop();
};
}