How to prevent a user from having multiple instances of the Same Web application

前端 未结 16 685
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 05:39

I\'m wondering if it is possible to determine if a user already has a web browser open to the web application I\'m working on. It seems that they can open several instances

16条回答
  •  广开言路
    2020-12-06 06:08

    All you have to do is assign a value both to a Hidden Input control's value and to a session variable in the Page Load event and on postback, check the value of the local variable against the value in the session variable. If the values do not match, you can redirect the user to the login page or a page that tells them that the session for that page is no longer valid etc.

    Example:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                'SET LOCAL VARIABLE AND SESSION VARIABLE TO A UNIQUE VALUE
                'IF THE USER HAS CHANGED TABS THIS WILL CHANGE THE VALUE OF THE SESSION VARIABLE
                Me.HiddenInput.value = New Guid().ToString
                Me.Session.Add("PageGuid", Me.HiddenInput.value)
    
            Else 
                'ON POSTBACK, CHECK TO SEE IF THE USER HAS OPENED A NEW TAB
                'BY COMPARING THE VALUE OF THE LOCAL VALUE TO THE VALUE OF THE SESSION VARIABLE
    
                If me.HiddenInput.value <> CType(Session("PageGuid"), String) Then
    
                    'THE VALUES DO NOT MATCH, MEANING THE USER OPENED A NEW TAB.
                    'REDIRECT THE USER SOMEWHERE HARMLESS
    
                    Response.Redirect("~/Home.aspx")
    
                Else
    
                    'THE VALUES MATCH, MEANING THE USER HAS NOT OPENED A NEW TAB
                    'PERFORM NORMAL POSTBACK ACTIONS
    
                    ...
    
                End If
            End If
        Catch ex As Exception
            Me.Session.Add("ErrorMessage", BusinessLogic.GetErrorMessage(ex))
            Me.Response.Redirect("~/ErrorPage.aspx", False)
        End Try
    End Sub
    

提交回复
热议问题