Hello
I'm tring to Create HttpModlue to trace userInfo when web application starts.
However I have some problems where I tried ot call MappingUserInfo() under Application_BeginRequest.
When I tried to call MappingUserInfo(), there is null
CType(HttpContext.Current.Cache(uInfo.CacheKey), UserInfo).SessionID
which I already get the sessionID in
Application_PreRequestHandlerExecute
It worked fine when I do Session_Start with Global.ascx but I would like to some work in HttpModule.
Any help would be greatly appreciated.
Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
AddHandler application.PreRequestHandlerExecute, AddressOf Me.Application_PreRequestHandlerExecute
AddHandler application.BeginRequest, AddressOf Me.Application_BeginRequest
AddHandler application.EndRequest, AddressOf Me.Application_EndRequest
End Sub
Private Sub Application_BeginRequest(ByVal [source] As [Object], ByVal e As EventArgs)
Dim application As HttpApplication = CType([source], HttpApplication)
Dim context As HttpContext = application.Context
MappingUserInfo()
End Sub
Private Sub Application_EndRequest(ByVal [source] As [Object], ByVal e As EventArgs)
Dim application As HttpApplication = CType([source], HttpApplication)
Dim context As HttpContext = application.Context
End Sub
Private Sub Application_PreRequestHandlerExecute(ByVal [source] As [Object], ByVal e As EventArgs)
Dim application As HttpApplication = CType([source], HttpApplication)
Dim handler As IHttpHandler = application.Context.Handler
If TypeOf handler Is Page Then
AddHandler DirectCast(handler, System.Web.UI.Page).PreRender, AddressOf Me.CustomModule_PreRender
End If
End Sub
Private Sub CustomModule_PreRender(ByVal [source] As [Object], ByVal e As EventArgs)
Try
Dim uInfo As New UserInfo()
uInfo.SessionID = HttpContext.Current.Session.SessionID
Catch ex As Exception
End Try
End Sub
Private Sub MappingUserInfo()
Try
Dim uInfo As New UserInfo()
uInfo.SessionID = DirectCast(HttpContext.Current.Cache(uInfo.CacheKey), UserInfo).SessionID
uInfo.UserName = WindowsIdentity.GetCurrent().Name
uInfo.StartTime = DateTime.Now.ToString()
uInfo.MachineName = GetMachineName
uInfo.IpAddress = GetIpaddress
Dim SessTimeOut As New TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0)
HttpContext.Current.Cache.Insert(uInfo.CacheKey, uInfo, Nothing, DateTime.MaxValue, SessTimeOut)
Catch ex As Exception
Throw New Exception("Error in Session_Start function", ex)
End Try
End Sub