Asp.net HttpContext处理程序

Asp.net HttpContext处理程序,asp.net,httphandler,httpcontext,Asp.net,Httphandler,Httpcontext,为了保护下载文件,我创建了如下http处理程序: public void ProcessRequest(HttpContext context) { string user = HttpContext.Current.User.Identity.Name; FilePermissionHelper helper = new FilePermissionHelper(user); string path = context.Request.F

为了保护下载文件,我创建了如下http处理程序:

  public void ProcessRequest(HttpContext context)
  {
        string user = HttpContext.Current.User.Identity.Name;
        FilePermissionHelper helper = new FilePermissionHelper(user);
        string path = context.Request.Form["file"];
        bool canDownload = helper.HasPermission(FileOperation.Download, path);
        if (!canDownload)
        {
            context.Response.StatusCode = 403;
            context.Response.End();
            return;
        }
        else
        {
            string fileName=String.Format(@"{0}App_Data\files{1}",HostingEnvironment.ApplicationPhysicalPath,path.Substring(1));
            context.Response.ContentType = "application/octet-stream";
            context.Response.AppendHeader("Content-Disposition", fileName);
            context.Response.TransmitFile(fileName);
            context.Response.End();
        }
 }
它使用HttpContext.Current.User

当我将此处理程序用于服务文件时,例如:

  protected void tvFile_NodeClick(object sender, RadTreeNodeEventArgs e)
  {
        string url = new Uri(String.Format("{0}/{1}", Request.Url.GetLeftPart(UriPartial.Authority),HandlerName)).AbsoluteUri;
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        string data = String.Format("file={0}", e.Node.Value);
        byte[] buffer = Encoding.UTF8.GetBytes(data);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = buffer.Length; 
        Stream reqst = req.GetRequestStream(); 
        reqst.Write(buffer, 0, buffer.Length);
        reqst.Flush();
        reqst.Close();
        byte[] bytes=ReadFully(((HttpWebResponse)req.GetResponse()).GetResponseStream());
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}",e.Node.Text));
        HttpContext.Current.Response.BinaryWrite(bytes);
        HttpContext.Current.Response.End();

 }

我在处理程序中得到了HttpContext.Current.User=null。当然,我可以使用POST数据、会话,但我希望通过HttpContext解决此问题。顺便说一句,当我在客户端(通过js)发表文章时,一切都正常:HttpContext.Current.User已传递给处理程序。发生了什么?谢谢。

使用
IHttpHandler
向处理程序添加另一个名为
IRequiresSessionState
的接口,您可能会正确获得用户项目

使用
IHttpHandler
向处理程序添加另一个名为
IRequiresSessionState
的接口,您可能会正确获得用户项目