Search code examples
c#asp.netvb.netgeneric-handler

Get Username with ASP.NET Generic Hanlder


I'm trying to get username of currely logged in user by ASP.NET Generic Handler (.ashx). But the username is always nothing even though the user is currently logged in.

Here's my Generic Handler class:

Imports System.Web.SessionState

    Public Class FileUploadHandler
    Implements System.Web.IHttpHandler
    Implements IRequiresSessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        'username is always null, i'm not sure why there's nothing eventhough the user is logged in     
        Dim username = context.Current.User.Identity.Name

        If String.IsNullOrEmpty(username) Then
            context.Response.StatusCode = 0
        Else
            context.Response.StatusCode = 1

        End If

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

But I can get the username of the logged in user from any page like this:

Dim username = Context.Current.User.Identity.Name

Do you think what it is the problem here? I'm OK with both C# and VB.NET. Thanks.


Solution

  • 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