allow only one system to login the aspx page

RAVI 1,076 Reputation points
2025-01-27T11:19:39.5733333+00:00

Hello

I have hosted my aspx pages in serevr its accesing using local ip 192.168.xx.x for example i have abc.aspx it should allow only one system and one login to access if any other same user login and coming from difference browser or system has to show alert message already using in another system

how to check that on page load asp.net c#

Thanking You

Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 5,586 Reputation points
    2025-01-27T20:31:16.9633333+00:00

    Knowing if a user is still logged in or is using a page can be a challenge!

     

    The issue is how the web works. It is a disconnected and so called “state less” type of system.

     

    To make a long story short?

    While on that page abc.aspx?

    I might turn off my computer.

    I might use my browser book marks and start shopping on Amazon.

    I might close my browser.

    I might jump to another page.

     

    In ALL of the above examples, the server has no idea that the page is open anymore or not.

     

    So, trying to check on page load not really going to work, since once the page is sent client side, then on the server side that “code behind” and code is 100% removed from memory, does not exist anymore.

    And most often?

    A user will not use the provided logoff option from the menu.

    So, once again, you (well, the server) has no idea, and in fact no real means to “know” that page is not open or being viewed anymore, does it?

     

    And that “state less” architecture means that for a given page, once rendered, then the code behind, variables etc. are 100% disposed of, removed from memory.  So, page load has no idea if someone else just loaded the page, since code etc. is recreated from scratch each time the page loads. I can’t stress how important this concept is (that code behind page class ONLY exists in memory during the page load and render process). Once the page is sent to client side, then on server side, that code and variables etc. are 100% tossed out of memory, and does not exist in memory server side anymore.

     

    Remember, the web server is not just dishing out pages to ONE user, but many users. Once a page is rendered, sent to the client, then the web server is now just waiting for ANY user to post-back a page, and the server has "zero" knowledge that some page is loaded or that the browser is even open anymore.

    So, unlike desktop programs? (with one user, one computer, and one copy of the code running), for web based, you really don’t know if that page is still open or not.

    So, if you decide to say lock out a user for a given page?

    Well, then how you going to know the page is still open?

    Answer:

    You don’t!

     

    And thus, we ALSO don’t know if the user is logged on anymore, do we? We simply don’t know. I suppose we could wait until session times out – but that’s often 20 minutes, or even more.

     

    However, often we most certainly want to know how many users are logged on to our web site. And after attempting many things over the years?

     

    The simple approach we adopted was to add a “keep alive” or so called “heartbeat” approach.

     

    So, in our master page, we include a wee bit of JavaScript, and it triggers every 90 seconds.

    I suppose we could reduce that time down to say 15 seconds. But, we did not want to cause any real noticeable “extra load” on the server when we have say 50 users on the site. So, for us, a 90 second time resolution was sufficient for our needs.

    So, the above approach can used to tell if the user is logged on, or in your case, still on the given web page.

     

    The code is really simple, but you need to save/store/persist that the user is still active and logged on. Or, in this case, is still activity using and has that one page open.

    I thus suggest using the database for this, since that type of storage that persists very well.

     

    So, in our master page (or in this case your abc.aspx page), then add a web method.

     

    We display the time of day in the upper right of our applications, and thus update the time of day every 90 seconds. So, we kind of “double purpose” the updating of time on the page and use it for both “keep alive” and that of time update.

     

    Really wonderful with this approach is that a call to any web method re-sets the user’s timeout, and re-sets the length (time) of the session() expire time.

    Thus, the web server’s session/logout time settings are 100% ignored. We thus  can now set (override) the timeout for users at our will.

    So, here is a working proof of concept:

     

    Our table:

    User's image

    And our markup:

     

        <form id="form1" runat="server">
    
            <div style="padding:35px">
                <asp:Label ID="lblTime" runat="server" Text=""
                    style="float:right"
                    ClientIDMode="Static">
                </asp:Label>
                <br />
    
    
    
            </div>
        </form>
    
        <script>
    
            $(window).on('load', function () {
                // update time in menu bar
                UpDateTime()
                setInterval(UpDateTime, 15000) // 15 secound window testing 
    
            })
    
            function UpDateTime() {
                $.ajax({
                    type: "POST",
                    url: '/Test3/Abc.aspx/MyTick',
                    data: {},
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    success: function (mydata) {
                        $('#lblTime').text(mydata.d)
                    },
                    error: function (request, status, error) {
                        var r = request.responseText
                        var errorMessage = r.Message
                        console.log(error)
                    }
                });
            }
        </script>
    
    

     

    And now our code behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                If PageLocked() Then
                    ' do whatever for this page not being allowed.
                    Response.Redirect("~/")  ' for now, just navagate to home page
                End If
            End If
    
        End Sub
    
        Public Function PageLocked() As Boolean
    
            Dim strSQL As String =
                $"SELECT * FROM PageLockOut WHERE PageToLock = '{sPageToLock}'"
    
            Dim dtLock = MyRst(strSQL)
            If dtLock.Rows.Count > 0 Then
                Dim MyTimeDif As Integer = DateDiff(DateInterval.Second,
                                                    dtLock.Rows(0)("LastHeartBeat"),
                                                    DateTime.Now)
    
                ' if tick time been less then 90 seconds, then page is being used......
                If MyTimeDif <= 90 Then
                    Return True
                End If
            End If
    
            Return False
    
        End Function
    
    
        <WebMethod>
        Public Shared Function MyTick() As String
    
            Dim sTime As String = Date.Now.ToString("hh:mm tt")
    
            Dim sPageToLock As String = "Abc.aspx"
    
            Dim strSQL As String =
                $"SELECT * FROM PageLockOut WHERE PageToLock = '{sPageToLock}'"
    
            Dim dtLock As DataTable = MyRst(strSQL)
            Dim MyRow As DataRow
    
            If dtLock.Rows.Count = 0 Then
                MyRow = dtLock.NewRow
                MyRow("PageToLock") = sPageToLock
                dtLock.Rows.Add(MyRow)
            Else
                MyRow = dtLock.Rows(0)
            End If
    
            MyRow("LastHeartBeat") = DateTime.Now
            SaveData(dtLock, strSQL)
    
            Debug.Print($"Server tick, rows = {dtLock.Rows.Count}")
            Return sTime
    
        End Function
    
    

    So, you can choose say 90 seconds, or maybe 30 seconds. The web method will work and run behind the scenes, and the page will not flicker or post back. So, the above is one approach you can use. If the user closes the page, turns off their computer, or navigates to another URL? Then the time tick will stop updating, and thus you can well test/know if someone is using that page (say in the last 90 or 30 seconds, or whatever time interval you wish).

    This does of course mean if someone exits the page, then they also have to wait 90 seconds before they can "get back in" and use that page.

     

     

     

     


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.