对网站所有网页验证登录

首先在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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值