繁体   English   中英

HttpContext.Current.User.Identity.Name在.ashx处理程序中不起作用

[英]HttpContext.Current.User.Identity.Name not working in .ashx hander

我无法从处理程序文件中的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;
        }
    }

}

数据没有传递给它吗?

不要期望我们能够告诉您这一点。 您是调用处理程序的人,因此要取决于您是否要传递身份验证cookie。

话虽如此,看来您的处理程序正在处理文件上传。 调用它的方式对我们来说完全是个谜,但是如果您使用某些依赖Flash的客户端上载组件(例如Uploadify) ,则该组件可能不会随请求一起发送身份验证和会话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 
{
...
}

如果您使用的是任何客户端组件,例如UploadifyPlupload ,则可能是该组件未随请求发送身份验证和会话cookie。 这里有一个很好的解释来解决

使用ASP.NET检出Uploadify(会话和身份验证)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM