Check Session variable and Redirect to login page before page load

后端 未结 4 1408
旧时难觅i
旧时难觅i 2021-01-07 13:05

How can I check a variable and redirect to another page before the page loads using ASP.NET?

I\'m aware of the life cycle, and PageInit() sounds like it

4条回答
  •  青春惊慌失措
    2021-01-07 13:35

    you can place this code anywhere in the masterpage to check a variable and redirect to another page before the content page loads.

    protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
    
            if (string.IsNullOrEmpty(Convert.ToString(Session["email"])) ||string.IsNullOrEmpty(Convert.ToString(Session["mobile"])))
            {
                Response.Redirect("Login.aspx");
            }
        }
    

提交回复
热议问题