本文介绍了HttpContext.Current.User.Identity.Name不工作在ashx的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从HttpContext.Current.User.Identity.Name在我的处理程序文件的响应。是数据不被传递给它?

 <%@ WebHandler LANGUAGE =C#级=uploadHandler%>

使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;


公共类uploadHandler:IHttpHandler的{

    公共无效的ProcessRequest(HttpContext的背景下)
    {
        HttpPostedFile文件= context.Request.Files [的FileData];

        字符串targetLocation =D:\\ \\的Inetpub \\的wwwroot \\ upload.website.com WWW \\上传\\+ 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);
    }

    公共BOOL IsReusable {
        得到 {
            返回false;
        }
    }

}
 

解决方案

别指望我们能告诉你这一点。你是第一个调用处理程序,所以它的你来知道你是否正在传递一个身份验证cookie与否。

这虽这么说,似乎你的处理程序处理文件上传。您所呼叫的方式是一个完整的神秘给我们,但如果你使用的是一些客户端上传组件,如 Uploadify 依赖于Flash中,该组件可能无法发送认证和会话Cookie随请求。

有变通方法在本博客帖子。在此类似的问题。

I cannot get a response from HttpContext.Current.User.Identity.Name in my handler file. Is the data not being passed to it?

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

}
解决方案

Don't expect us being able to tell you this. You are the one calling the handler, so it's up to you to know whether you are passing an authentication cookie or not.

This being said it seems that your handler deals with file uploads. The way you are calling it is a complete mystery to us but if you are using some client side upload component such as Uploadify that relies on Flash, this component might not be sending authentication and session cookies along with the request.

There are workarounds as explained in this blog post. Also discussed in this similar question.

这篇关于HttpContext.Current.User.Identity.Name不工作在ashx的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:20
查看更多