1

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

}
jrummell
  • 42,637
  • 17
  • 112
  • 171
user547794
  • 14,263
  • 36
  • 103
  • 152
  • 1
    Does your web site require you to login? Also, you don't need `HttpContext.Current`, you can use the `context` parameter - `context.User.Identity.Name`. – jrummell Mar 01 '12 at 21:37

4 Answers4

3

Is the data not being passed to it?

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have been struggling all day trying to figure out why authentication is working on every request except my file upload endpoint. Your comment just made me realize this is an exception where I am not using my http wrapper to send ajax requests using an auth token (instead I am posting directly from the html form). Thank you! Life saver. So glad I stumbled upon this. – Ryan Apr 22 '16 at 04:42
2

The class should perhaps also implement the interface IRequiresSessionState, if you need the users Session object. But perhaps the context is enough for you?

public class uploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{
...
}
Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17
1

Use parameter context instead!

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

turns into

context.Response.Write(context.Current.User.Identity.Name);
CodeZombie
  • 5,367
  • 3
  • 30
  • 37
  • 2
    That shouldn't make a difference as to whether or not `User.Identity.Name` is set, but I would still make this change. – jrummell Mar 01 '12 at 21:47
0

If you are using any client side component such as Uploadify, Plupload, it can be that the component is not sending authentication and session cookies with the request. There is a good explanation here for workaround.

Check out Uploadify (Session and authentication) with ASP.NET

Community
  • 1
  • 1
Tola
  • 2,401
  • 10
  • 36
  • 60