簡體   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