Tad*_*aia 14 asp.net asp.net-mvc-3 mvc-mini-profiler
我尝试使用此代码检查用户是否在Application_BeginRequest和Application_AuthenticateRequest中扮演角色,但它不起作用.在BeginRequest中,代码永远不会被命中,并且使用某些请求对其进行身份验证,并且探查器不会显示.
只检查Request.IsLocal工作正常.
if(Request.IsAuthenticated)
{
if(User.IsInRole("Admin");
MiniProfiler.Start();
}
Run Code Online (Sandbox Code Playgroud)
任何想法或为什么它没有工作或更好的方式来做到这一点?
[更新]我接受了芒果,但解除了它,因为我没有完全做到这一点
我做了以下但是探查器最初没有出现.经过几次尝试后,它开始出现,即使我试图以隐身模式访问网站,所以没有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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在检查
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();
}
}
Run Code Online (Sandbox Code Playgroud)
并在请求结束时结束.
protected void Application_EndRequest()
{
MvcMiniProfiler.MiniProfiler.Stop();
}
Run Code Online (Sandbox Code Playgroud)
[Update2]结束问题,忽略这一点,我被outputcache所拥有.
Sam*_*ron 17
cookie feanz提到是一个方便的技巧,第二种方法是无条件分析,然后放弃未经身份验证的用户的会话:
protected void Application_BeginRequest()
{
MvcMiniProfiler.MiniProfiler.Start();
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if(!CurrentUserIsAllowedToSeeProfiler())
{
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
}
Run Code Online (Sandbox Code Playgroud)
在请求生命周期中对用户进行完全身份验证之前,会发生开始请求.
我通过添加cookie解决了这个问题,如果用户处于角色(在您的情况下为"Admin"),当请求被验证时,您可以在开始请求时检查此cookie并初始化分析器.
它不会第一次工作,但每次都应该在那之后工作.
小智 5
这是我的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);
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1684 次 |
最近记录: |