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

asp.net c#仅使用全局asax限制登录用户对文件夹的访问,c#,asp.net,security,directory,C#,Asp.net,Security,Directory,我试图约束用户和搜索引擎不要靠近登录用户上传文件的文件夹,只有登录用户才有权查看文件。 我必须使用全局asax设置来实现这一点,因为任何其他方法(如成员资格框架)在我的生活中已经太晚了。 当用户登录会话时(thisuser)被激发-因此我尝试了许多不同的方法: protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) { string url = HttpContext

我试图约束用户和搜索引擎不要靠近登录用户上传文件的文件夹,只有登录用户才有权查看文件。 我必须使用全局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)
尽管我做出了不懈的努力,但全球asax仍然没有进行任何合作;我对global asax的所有请求都被置若罔闻,导致(1)访问所有用户(2)或System.NullReferenceException。 我该怎么做才能让global asax开始听我说?请告知。

您的代码输入错误(可能不是问题的唯一原因,但您应该尝试)


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


不过,我不认为这是一个安全的解决方案。

谢谢。你能分享一些关于为什么这不够安全的想法吗。如果我在global asax中明确限制对文件夹的访问,它仍然会秘密地与流氓机器人/搜索引擎/和用户联手,并允许在表下进行访问?在SO中键入时,打字错误是我的错误。全球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");
            }                                
        }
    }