Asp.net Vb.net,访问控制并发

时间:2016-07-21 16:43:42

标签: asp.net vb.net concurrency

大家好我试图限制我的WebApp上使用框架Asp.net运行的WebApp。换句话说,我如何控制允许的会话数。

WebApp运行良好,现在我想添加"有多少用户可以访问WebApp" if用户数"会话"超出新的用户将被重定向到一个网站。

我已经为此工作了几个星期......尝试过Cookies和季节而且都没有工作

场景:最大用户可以访问WebApp是1

当我启动第二个浏览器并启动新会话时,UserNumber将增加1,因此将被重定向到" ServerBusy"页...

但是,如果第二个浏览器打开了新选项卡并访问了将在Session_Start中绕过条件的WebApp,并将访问WebApp。

谢谢。

这是代码

Imports System.Web

Public Class Global_asax
Inherits System.Web.HttpApplication
Public Shared maxUsersAllowance As Integer = 1 'Total number of users can enter the server
Public Shared UserNumber As Integer = 0 ' the current number of users

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the application is started
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    UserNumber = UserNumber + 1
    Dim myCookie As New HttpCookie("UserNumber")
    Dim myCookie2 As New HttpCookie("Access")
    myCookie.Value = UserNumber
    myCookie2.Value = True
    Response.Cookies.Add(myCookie)
    Response.Cookies.Add(myCookie2)
    If (Request.Cookies("UserNumber") IsNot Nothing) Then
        If (Request.Cookies("UserNumber").Value > 1) Then
            If (Request.Cookies("Access").Value) Then
                Response.Redirect("~/ServerBusy.aspx")
            End If
        End If
    End If

End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)


End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Sub Application_Init(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Sub Application_Dispose(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Sub Application_Unload(ByVal sender As Object, ByVal e As EventArgs)

End Sub

End Class

阅读文章https://stackoverflow.com/a/6218525/1260204并更新代码后,问题仍然存在,即用户可以在浏览器上打开新标签并进行连接后进入WebApp

Imports System.Web

Public Class Global_asax
Inherits System.Web.HttpApplication



Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the application is started
    Application("ActiveSessions") = 0
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

    Try
        Application.Lock()

        Dim activeSessions As Integer = CInt(Application("ActiveSessions")) + 1
        Dim allowedSessions As Integer = 1
        ' retrieve the threshold here instead
        Application("ActiveSessions") = activeSessions

        If activeSessions > allowedSessions Then
            System.Web.HttpContext.Current.Response.Redirect("~/ServerBusy.aspx", False)
        End If
    Finally
        Application.UnLock()
    End Try
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)


    Dim LiveSessionsCount As Integer = CInt(Application("LiveSessionsCount"))
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)

End Sub


Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

    Application.Lock()
    Application("ActiveSessions") = CInt(Application("ActiveSessions")) - 1
    Application.UnLock()


End Sub

End Class

更新,我已用标签解决了问题。通过增加     Session.Abandon() 在我的重定向页面中。然而,就我可以达到的目的而言,这是一半的解决方案...另一半是,如果用户通过X关闭他的浏览器,我需要等待20分钟,直到会话结束...是否需要终止一旦用户退出/终止页面会话?

0 个答案:

没有答案