简体   繁体   English

我如何只允许一个访客访问页面

[英]how can i allow only one visitor to visit the page

I have a page call "A" Asp.Net and i want to allow only one user to access the page. 我有一个页面调用“ A” Asp.Net,我只允许一个用户访问该页面。 If nobody accessing the page then any user are allowed to access the page Without using database how can i do it. 如果没有人访问该页面,则允许任何用户访问该页面而不使用数据库,我该怎么做。 I am trying to use page load of c# and window onbeforeunload event of javascript but didnt get it. 我正在尝试使用C#的页面加载和JavaScript的onbeforeunload事件窗口,但没有得到它。

<script type="text/javascript" language="JavaScript">
        var hidViewstate;

        window.onload = body_Load;
        window.onbeforeunload = WindowCloseHanlder;

        function body_Load()
        {
            hidViewstate = document.getElementById('<%=hidViewstate.ClientID %>');
        }

        function WindowCloseHanlder()
        {
            hidViewstate.value = parseFloat(hidViewstate.value - 1);
        }

    </script>

<div>
        <asp:Label ID="lblText" runat="server" Text="Label"></asp:Label>
        <asp:HiddenField ID="hidViewstate" runat="server" />
    </div>

protected void Page_Load(object sender, EventArgs e)
    {
        hidViewstate.Value = "1";

        if(Convert.ToInt32(hidViewstate.Value) == 1)
        {
            lblText.Text = "Welcome First User";
        }
        else
        {       
            lblText.Text = "Visiting on this page is not allowed because user is already login.";

        }

    }

This is counter to how a web server should operate. 这与Web服务器应如何操作相反。 It would be interesting to know your actual problem that led you to try this. 了解导致您尝试此操作的实际问题会很有趣。

On the technical side you could store a global variable in Application . 在技​​术方面,您可以将全局变量存储在Application All requests share the same global object. 所有请求都共享同一个全局对象。

But if you explained to us the real problem you are trying to solve I think you would find this isn't a good approach and Im sure we could suggest a real solution. 但是,如果您向我们解释了您要解决的真正问题,我认为您会发现这不是一个好方法,我相信我们可以建议一个真正的解决方案。

You can use a global veriable, like static variable of a static class, 您可以使用全局验证,例如静态类的静态变量,

on page load you can check like 在页面加载中,您可以像

if(clsGlobal.userCount > 1)
{
Response.Redirect("~/anotherPage.aspx") // somewhere you want..
}
else
{
//display your welcome message
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM