asp.net c# 将文件夹访问权限限制为仅使用全局 asax 的登录用户

本文关键字:全局 asax 用户 登录 net 文件夹 访问 权限 访问权 asp | 更新日期: 2023-09-27 17:57:06

我正在尝试约束用户和搜索引擎不要靠近登录用户上传文件的文件夹,只有登录用户才能查看文件。我必须使用全局 asax 设置来实现这一点,因为任何其他方法(例如成员资格框架)在我的生活中都变得太晚了。当用户登录会话(thisuser)时被触发 - 所以我尝试了许多变体:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) 
    {
        string url = HttpContext.Current.Request.Url.AbsoluteUri;
         url = url.ToUpper();
         if (System.Web.HttpContext.Current.Session == null && url.Contains("CONFIDENTIAL"))
        {                
                Response.Redirect("Login.aspx");                                
        }
        if (System.Web.HttpContext.Current.Session != null && url.Contains("CONFIDENTIAL"))
        {                    
            if (Session["THISUSER"].ToString() != "OK")
            {
                Response.Redirect("Login.aspx ");
            }                                
        }
    }
//I tried  using below also with pretty much same logic as above 
     void Application_BeginRequest(object sender, EventArgs e)
//and also
      void Application_AcquireRequestState(object sender, EventArgs e)

尽管我不懈努力,但 global asax 完全不合作;我对 global asax 的所有请求都被置若罔闻,导致 (1) 访问所有用户 (2) 或 System.NullReferenceException。我该怎么做才能让全球 asax 开始倾听我的声音?请指教。

asp.net c# 将文件夹访问权限限制为仅使用全局 asax 的登录用户

代码

中的拼写错误(可能不是问题的唯一原因,但您应该尝试)

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) 
    {
        string url = HttpContext.Current.Request.Url.AbsoluteUri;
         url = url.ToUpper();
         if (System.Web.HttpContext.Current.Session == null && url.Contains("CONFIDENTIAL"))
        {                
                Response.Redirect("Login.aspx");                                
        }
        if (System.Web.HttpContext.Current.Session != null && url.Contains("CONFIDENTIAL"))
        {                    
            if (Session["THISUSER"].ToString() != "OK")
            {
                Response.Redirect("Login.aspx");
            }                                
        }
    }

如果会话中不存在"THISUSER",则会发生 NullReferenceException。 您还应该在字符串上使用 ToUpperInvariant()。

但是,我不相信这实际上是一个安全的解决方案。