Check Session variable and Redirect to login page before page load

坚强是说给别人听的谎言 提交于 2019-11-30 21:05:49

问题


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 would be right, but I can't seem to find anywhere to put the code without and error within Visual Studio.

I can't put the onpageinit="" within the first line of my page declaration. Am I suppose to put it somewhere different? My page declaration looks like:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dashboard.aspx.cs" Inherits="dashboard" MasterPageFile="~/Design.master" %>

This is the code that I want to run on the page load:

    // Check if the user is logged in, if not send them to the login page
    if (session.logged_in == false)
    {
        // Redirect the user to the login page
        Response.Redirect("login.aspx");

    }

回答1:


You have to override the OnInit method of the page. Place this just above (order doesn't matter, but I believe organization is important) your Page_Load event in your code behind...

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    // your code goes here
}

Also, depending on what you are trying to accomplish, I would suggest looking into FormsAuthentication. With that you can simply specify secure folders and a login page and .NET handles kicking the visitor to the login page if they are not authenticated.




回答2:


I suggest you take the time to read up about Asp.Net membership. Then if you need to, implement your own MembershipProvider that will take care of the necessary plumbing with regards to authenticating user requests.

The membership abstraction is really useful and implementing your own provider is really not that difficult.

That said, if you really want to use a value from the Session to manage authentication, you could try to put the code in the Application_PostAcquireRequestState method in Global.asax. That way your code will automatically be called for (almost) all request to your application, and it is also the first available event where the session is available (as far as I know). Sample:

void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    if (Session["LoggedUserName"] == null && !Request.Path.EndsWith("login.aspx"))
    {
        Response.Redirect("~/your/path/login.aspx");
    } 
}



回答3:


I'm not aware of an onpageinit attribute. The session variable is independent of the page life cycle. Session is always available. Assuming you always use the same master page, insert your code in Pre_Init in the code behind of the Master Page.

To do this, add the override to the code behind:

 protected override void OnPreInit(EventArgs e)
    {
        if (session.logged_in == false)
        {
          Response.Redirect("login.aspx", false);            
        }
    }


来源:https://stackoverflow.com/questions/11988457/check-session-variable-and-redirect-to-login-page-before-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!