HttpContext.Current.User.Identity.Name无法在.ashx hander中工作

时间:2012-03-01 21:33:33

标签: c# asp.net .net

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

<%@ WebHandler Language="C#" Class="uploadHandler" %>

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;
        }
    }

}

4 个答案:

答案 0 :(得分:3)

  

数据是否未传递给它?

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

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

this blog post中解释的解决方法。这也在similar question中进行了讨论。

答案 1 :(得分:1)

使用参数context代替!

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

变成

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

如果您正在使用任何客户端组件,例如UploadifyPlupload,则组件可能不会根据请求发送身份验证和会话Cookie。这里有一个很好的解释workaround

查看Uploadify (Session and authentication) with ASP.NET

相关问题