【问题标题】:Global.ascx function to detect authenticated users first page visitGlobal.ascx 函数检测认证用户的首页访问
【发布时间】:2013-10-17 06:06:56
【问题描述】:

我目前正在使用 Global.ascx 文件中的“Session_Start”函数来保存经过身份验证的用户访问我的网站的时间。

如果用户会话过期,这可以正常工作,但是由于我使用的是持久性 cookie,因此用户可能会在 28 天内返回站点,并且不会调用此函数,因此不会记录在用户拥有的数据库中访问过。

我仔细查看了 Global.ascx 中所有可用的功能,但是我找不到可以执行我需要的功能。

Application_Start - 仅在生命周期内触发运行 Application_BeginRequest - 每个请求 Application_AuthenticateRequest - 每个请求 Session_Start - 当一个新的会话开始时

我认为可以使用的两个事件是 Application_BeginRequest 或 Application_AuthenticateRequest。

有没有办法限制上述事件只在第一次访问网站时运行特定代码,而不是在每次请求时运行?

或者有什么方法可以使用我的主文件吗?

任何建议都会非常有用。

干杯

【问题讨论】:

    标签: c# forms-authentication session-cookies global-asax


    【解决方案1】:

    为什么不自己实现呢?正如您提到的,有一个事件 Application_BeginRequest。我认为以下可能会奏效:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {       
        string session_param_name = "SOME_SESSION_ID";        
    
        if (HttpContext.Current.Request.Form[session_param_name] == null)
        {
             //Count
        }
        else if (HttpContext.Current.Request.QueryString[session_param_name] == null)
        {
            //Also count
         }
    }
    

    【讨论】:

    • 所以当一个 auth 用户第一次访问该站点时,比如说 example.com 不会有任何 Request.Form 或 Request.Querystring 变量。
    猜你喜欢
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2016-11-29
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多