如何正确验证MVC-迷你分析器与AspNetSqlMembershipProvider [英] How to properly authenticate mvc-mini-profiler with AspNetSqlMembershipProvider

查看:111
本文介绍了如何正确验证MVC-迷你分析器与AspNetSqlMembershipProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查,如果用户是在和的Application_BeginRequest作用Application_AuthenticateRequest此code和它不会工作。在对的BeginRequest code是从来没有击中并验证它击中了一些要求和探查显示不出来。

I tried to check if the user is in role at Application_BeginRequest and Application_AuthenticateRequest with this code and it will not work. At BeginRequest the code is never hit and Authenticate it's hit with some of the request and the profiler does not show up.

检查只针对Request.IsLocal正常工作。

Checking only for Request.IsLocal works fine.

if(Request.IsAuthenticated)
{
  if(User.IsInRole("Admin");
    MiniProfiler.Start(); 
}

任何想法,为什么它不工作或更好的方式来做到这一点?

Any idea or why it's not working or better way to do it?

[更新] 我接受了awnser但解开它,因为我并没有完全得到它做的工作。

[Update] I accepted the awnser but undid it as I didn't quite get it do work

我做了以下,但探查没有显示在第一次了。
试了几次后,它开始出现,甚至当我试图与接取隐身模式下的部位,所以没有饼干。

I did the following but the profiler is not showing up at first. After a few tries it started showing up, even when I tried to acess the site with incognito mode, so no 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

[Update2] Closing question, ignore this, I was being owned by outputcache.

推荐答案

开始请求发生之前用户在请求生命周期的全面验证。

Begin request happens before the user is fully authenticated in the request life cycle.

我加入一个cookie,如果用户是(在你的情况下,管理)角色时请求被验证,那么您可以检查在开始请求和初始化探查这个cookie解决了这个问题。

I solved this issue by adding a cookie if the user is in a role ("Admin" in your case) when the request is authenticated then you can check for this cookie on begin request and initialise the profiler.

据wont't后工作的第一次,但应该每一次。

It wont't work the first time but should every time after that.

这篇关于如何正确验证MVC-迷你分析器与AspNetSqlMembershipProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆