Imports Microsoft.VisualBasic
Imports System.Web.UI.Page
Imports System.Data.OleDb
Imports System.Data
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Public Class roots
    Inherits System.Web.UI.Page
    Protected Overrides Sub OnInit(e As EventArgs)
        If Session("username") = "" Then
            Response.Redirect("~/login.aspx")
        Else
          MyBase.OnInit(e)
        End If
    End Sub
End Class需要判定的页面直接添加以下代码Inherits roots类 用户登录 判定session为空

解决方案 »

  1.   

    当然,你这么做能解决问题,很好。但是,如果另外一个原因,你需要一套类似的代码(比如判断页面是否应该套用主题,你也用类似的方法实现),那么问题来了,VB不支持多继承,如果一个页面又需要主题,又需要登录,怎么继承呢?所以你会发现,其实我们不用这种办法,而是使用Http处理程序。
      

  2.   

    既然是总结贴
    那么我就给多一个方向好以后继续修炼
    AOP 面向切面编程
      

  3.   


    Namespace mynamespace
        Public Class myhandler
            Implements IHttpModule        Public Sub Init(application As HttpApplication) Implements IHttpModule.Init
                AddHandler application.AcquireRequestState, AddressOf application_AcquireRequestState
            End Sub        Public Sub application_AcquireRequestState(sender As Object, e As EventArgs)
                Dim app As HttpApplication = sender
                If app.Context.Session("username").ToString.Trim() = "" Then
                    app.Context.Response.Redirect("~/login.aspx")               
                End If
            End Sub        Public Sub dispose() Implements System.Web.IHttpModule.Dispose
            End Sub
        End Class
    End Namespace这是我使用ihttpmodule的方法,但始终无法实现上述功能,web.config也进行了添加(iis7):<modules>
          <add type="mynamespace"        name="myhandler" />
          </modules>
    大伙看看问题出在哪里?继续修炼,共同提高嘛!
      

  4.   

    怎么没人回答啊 ?  caozhy版主 帮我分析一下吧?
      

  5.   

    caozhy版主  在你的指点下 我实现了http处理session登录验证的功能,尽管经历2天时间。现共享经验!还有一个疑问就是为什么不能使用Response.Redirect进行跳转?
    Imports Microsoft.VisualBasic
    Imports System.Web
    Namespace Samples.AspNet.VB    Public Class SessionModule
            Implements IHttpModule
            Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
                AddHandler context.AcquireRequestState, AddressOf Me.context_AcquireRequestState
            End Sub        Private Sub context_AcquireRequestState(sender As Object, e As EventArgs)
                Dim app As HttpApplication = CType(sender, HttpApplication)
                Dim requestUrl As String = app.Context.Request.Path
                Dim filename As String = System.IO.Path.GetFileName(requestUrl)
                If app.Context.Session Is Nothing Then
                    Return
                End If
                If app.Context.Session("username") = "" Then
                    If filename <> "login.aspx" Then
                        app.Context.Server.Transfer("~/login.aspx") '不清楚为什么无法使用 app.Context.Response.Redirect("~/login.aspx")进行跳转?
                    End If
                End If
            End Sub
            Public Sub Dispose() Implements IHttpModule.Dispose
            End Sub
          End Class
    End Namespace