ASP.NET Webforms模块,上下文用户为null

时间:2014-07-21 18:13:45

标签: asp.net webforms ihttpmodule

当HTTP模块(下面的代码)处理视频请求时,例如/website/uploads/Video/M2U00001_2.mp4,_context.User为空。

当我使用Visual Studio开发服务器_context.User在我的本地计算机上的VS2010中运行它时。部署到IIS 7(.net 4.0)后_context.User为Null。

当http模块处理aspx页面时,'_ context.User'不是Null,但在处理javascript,图像,视频或CSS时它是Null。

任何人都可以解释为什么_context.User为空,并且可能的解决方案将确保_context.User不为空。

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

2 个答案:

答案 0 :(得分:0)

如果您实现IRequiresSessionState界面将拥有所有会话信息。这也会为您提供User信息。

在下面的代码段中,您会看到需要在代码中添加的内容。

Public Class VideoSecurityModule
    Implements IHttpHandler
    Implements IRequiresSessionState 'You must add this line

    'You must add this function
    Public ReadOnly Property IsReusable() As Boolean
        ' IsReusable must be set to false since class has a member!
        Get
            Return False
        End Get
    End Property
End Class

答案 1 :(得分:0)

要确保设置_context.User,请将runAllManagedModulesForAllRequests="true"添加到web.config的modules部分,如下所示。

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