首先在webconfig里面配置:
<httpModules>
<!-- 用來處理哪些頁面一 定要Login -->
<add name="LoginModule" type="NBeForm_WEB.LoginModule,NBeForm-WEB"/>
</httpModules>
然后通过以下类对页面进行判断页面是否需要登录以及是否已登录:
Imports System.Configuration.ConfigurationSettings
Public Class LoginModule : Implements IHttpModule
Private httpApp As HttpApplication
'In the Init function, register for HttpApplication
'events by adding your handlers.
Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
AddHandler application.BeginRequest, AddressOf Me.Application_BeginRequest
AddHandler application.EndRequest, AddressOf Me.Application_EndRequest
End Sub
'Your BeginRequest event handler.
'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
' Dim uid As String
' Dim url As String
' url = context.Current.Request.RawUrl
' If needLogin(url) Then
' uid = IIASCommon.Util.WebUtil.GetCookieValue("NBeFormLoginCookie", "UID")
' If uid = "" Then
' context.Response.Redirect("Login.aspx?ForwardPath=" & url)
' End If
' End If
' 'Implement custom code to append a custom guid string to your query string
'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
Dim uid As String
Dim url As String
Dim ur As Integer
url = context.Current.Request.RawUrl
ur = url.LastIndexOf("/") - url.IndexOf("/", 2) 'by 2008-01-11
If needLogin(url) Then
uid = IIASCommon.Util.WebUtil.GetCookieValue("NBeFormLoginCookie", "UID")
If uid = "" Then
If ur = 0 Then
context.Response.Redirect("Login.aspx?ForwardPath=" & url)
Else
context.Response.Redirect("../Login.aspx?ForwardPath=" & url)
End If
End If
End If
'Implement custom code to append a custom guid string to your query string
End Sub
'Your EndRequest event handler.
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
'Implement custom code for the end request
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 依所要求的URL判斷是否需Login
''' 不需Login的ASPX網頁要以大寫記在AppSetting的LoginExcludePath中
''' </summary>
''' <param name="path"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [kerr_lu] 2005/7/8 Created
''' </history>
''' -----------------------------------------------------------------------------
Private Function needLogin(ByVal path As String) As Boolean
Dim strExclude As String
Dim exclude() As String
Dim i As Integer
'strExclude = "LOGIN.ASPX,MENU.ASPX,HOME.ASPX"
strExclude = AppSettings.Get("LoginExcludePath")
exclude = strExclude.Split(",")
For i = 0 To exclude.Length - 1
If path.ToUpper.IndexOf(exclude(i)) > -1 Then
Return False
End If
Next
Return True
End Function
End Class