HttpContext.Current.User.Identity.Name无效.ashx hander

我无法从我的处理程序文件中获取HttpContext.Current.User.Identity.Name的响应。 数据没有传递给它吗?

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public class uploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { HttpPostedFile file = context.Request.Files["fileData"]; string targetLocation = "D:\\inetpub\\wwwroot\\upload.website.com\\www\\uploads\\" + HttpContext.Current.User.Identity.Name + "\\" + file.FileName; file.SaveAs(targetLocation); context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); context.Response.Write(HttpContext.Current.User.Identity.Name); } public bool IsReusable { get { return false; } } } 

数据没有传递给它吗?

不要指望我们能告诉你这个。 您是调用处理程序的人,因此您需要知道是否要传递身份validationcookie。

这就是说你的处理程序似乎处理文件上传。 您调用它的方式对我们来说是一个完全的谜,但如果您使用某些客户端上载组件(例如依赖于Flash的Uploadify) ,则此组件可能不会与请求一起发送身份validation和会话cookie。

有一些解决方法,如本博客文章中所述 。 在这个类似的问题中也讨论过。

请改用参数context

 context.Response.Write(HttpContext.Current.User.Identity.Name); 

变成

 context.Response.Write(context.Current.User.Identity.Name); 

如果需要用户Session对象,该类应该也可以实现接口IRequiresSessionState。 但也许上下文对你来说已经足够了?

 public class uploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { ... } 

如果您使用任何客户端组件(如Uploadify , Plupload) ,则组件可能不会发送带有请求的身份validation和会话cookie。 解决方法有一个很好的解释。

使用ASP.NET查看Uploadify(会话和身份validation)