C# 我可以用PostAuthorizerRequest方法启动MVC迷你探查器吗?
我使用的是MVC Mini Profiler,我只为处于“Profiler”角色的经过身份验证的用户显示Profiler。MiniProfiler.cs中的示例使用AuthenticateRequest方法来确定是否应该停止分析,但我将我的转换为使用PostAuthorizeRequest(在读取之后),以便能够访问IPrincipal和IsInRole方法。我可以在PostAuthorizerRequest方法中启动探查器,还是应该继续停止并放弃PostAuthorizerRequest中的结果?为每个请求启动和停止探查器的开销是多少 当前代码:C# 我可以用PostAuthorizerRequest方法启动MVC迷你探查器吗?,c#,asp.net,asp.net-mvc,mvc-mini-profiler,C#,Asp.net,Asp.net Mvc,Mvc Mini Profiler,我使用的是MVC Mini Profiler,我只为处于“Profiler”角色的经过身份验证的用户显示Profiler。MiniProfiler.cs中的示例使用AuthenticateRequest方法来确定是否应该停止分析,但我将我的转换为使用PostAuthorizeRequest(在读取之后),以便能够访问IPrincipal和IsInRole方法。我可以在PostAuthorizerRequest方法中启动探查器,还是应该继续停止并放弃PostAuthorizerRequest中的结
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();
};
}
我认为尽早启动探查器是很重要的(否则您可能会丢失一些关键信息,例如身份验证过程本身是否需要一段时间,或者某个HTTP模块是否存在问题)
由于
BeginRequest
事件发生在请求发生任何其他事件之前,因此这是开始分析的理想场所,然后在以后的步骤中决定是否保留分析的数据(PostAuthorize
,在您的情况下)。您可以随时使用调用放弃分析结果:
MiniProfiler.Stop(discardResults: true);
在StackOverflow,我们的“高性能”模式是:
应用程序_BeginRequest
-MiniProfiler.Start()中找到cookie李>
postAuthorizerRequest
之后:您的目标始终是尽早开始分析,并尽可能晚地停止分析。如果你只从管道的中间开始,管道可能停留在其中的部分不会被变形。p> 这是有道理的,我担心我的大多数用户不会看到分析器,因此我想知道从BeginRequest到PostAuthorizerRequest运行分析器会产生多少开销。我没有关于开销的详细信息,但我非常怀疑这一点,特别是如果在停止分析器时丢弃已分析的数据(我认为,将true传递给Stop()方法)。您可以始终对探查器进行分析,以获取有关探查器分析数据的分析数据。好的,因此您只能在cookies存在时启动探查器(可以在cookies登录时进行设置),然后在PostAuthorizeRequest中进行真正的验证。@Austin…是的…这是影响最小的方式太好了,谢谢!我真的很喜欢MiniProfiler,非常有用。
if (MiniProfiler.Current != null && !userReallyAuthenticated)
MiniProfiler.Stop(discardResults: true);