asp.netwebformsihttpmodule

ASP.NET Webforms Module, context user is null


When a video request is handled by the HTTP Module (code below) such as /website/uploads/Video/M2U00001_2.mp4 the _context.User is null.

When I run this in VS2010 on my local machine using the Visual Studio Development server _context.User is set. After I deploy to IIS 7 (.net 4.0) _context.User is Null.

'_context.User' is not Null when the http module processes an aspx page but it is Null when processing javascript, images, videos or CSS.

Can anyone explain why _context.User is null and possible solutions that will ensure _context.User is not null.

public Class VideoSecurityModule
    Implements IHttpModule

    Private WithEvents _context As HttpApplication

    Public Sub Dispose() Implements IHttpModule.Dispose

    End Sub

    Dim myUserManager As UserManager

    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        _context = context
        myUserManager = New UserManager
    End Sub

    Public Sub OnAuthorizeRequest(ByVal source As Object, ByVal e As EventArgs) Handles _context.PostAuthenticateRequest
        Const networkAuthenticationRequiredStatusCode As Integer = 511
        Try

            If IsVideoUrl() Then

                If _context.User Is Nothing Then
                    LogManager.WriteMessage("_context.User is nothing:", "")
                End If
                Dim userId As Integer = myUserManager.GetUserIdByUserName(_context.User.Identity.Name)
                If (UserRequiresAuthorization(userId)) Then

                    If Not UserIsAssignedToCourseContainingVideo(userId) Then
                        LogAccessDeniedMessage()
                        _context.Response.StatusCode = networkAuthenticationRequiredStatusCode
                        _context.Response.ClearContent()
                        _context.Response.Write("UnAuthorized User")
                        _context.Response.End()
                    End If
                End If
            End If
        Catch ex As Exception
          LogManager.WriteException(ex, "")
        End Try
    End Sub
End Class

Solution

  • To ensure that _context.User is set add the runAllManagedModulesForAllRequests="true" to the modules section of the web.config like below.

    <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ScriptModule" />
      <remove name="RadUploadModule" />
      <remove name="RadCompression" />
      <remove name="VideoSecurityModule" />
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule" preCondition="integratedMode,runtimeVersionv2.0" />
      <add name="RadCompression" type="Telerik.Web.UI.RadCompression" preCondition="integratedMode,runtimeVersionv2.0" />
      <add name="VideoSecurityModule" type="LMS.VideoSecurityModule"/>
    </modules>