且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

页面重定向到登录页面

更新时间:2022-04-14 03:01:24

使用Generic Handler [Global.asax]文件



C#代码



use Generic Handler[Global.asax] file

C# code

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Session["Authenticate"] = "";
        CheckLogin();
    }

    void Session_End(object sender, EventArgs e)
    {
       CheckLogin();

    }
    void Application_OnPostRequestHandlerExecute()
    {
       //CheckLogin();
    }

    void CheckLogin()
    {
        string Url = Request.RawUrl;
        int count = Url.Length - 10;
        string TestUrl = Url.Substring(count);
        string SessionData = Session["Authenticate"].ToString();
        if (SessionData == "" && TestUrl != "Login.aspx")
        {
            Response.Redirect("~/Login.aspx");
        }
    }
</script>







将代码放入登录页面。 cs



会话[认证] =是;




put the code in login page.cs

Session["Authenticate"] = "Yes";