检查Session变量和重定向页面加载之前到登录页面 [英] Check Session variable and Redirect to login page before page load

查看:119
本文介绍了检查Session变量和重定向页面加载之前到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查一个变量,并重定向到另一个页面中使用ASP.NET页面加载之前?

我知道的生命周期,而 PageInit()听起来这将是正确的,但我似乎无法找到任何地方把code没有和Visual Studio中的错误。

我不能把 onpageinit =我的网页声明的第一行中。是我该把它放在不同的地方?我的页面声明如下:

 <%@页面语言=C#AutoEventWireup =真codeFILE =dashboard.aspx.cs继承=仪表板的MasterPageFile =〜/设计的.master%>

这是code,我想在页面加载运行:

  //检查用户登录,如果不是送他们到登录页面
    如果(session.logged_in == FALSE)
    {
        //将用户重定向到登录页面
        的Response.Redirect(login.aspx的);    }


解决方案

您必须重写页面的OnInit方法。把这个正上方(顺序并不重要,但我相信组织是重要的)你的Page_Load事件中的code背后...

 保护覆盖无效的OnInit(EventArgs的发送)
{
    base.OnInit(E);    //你的code到这里
}

此外,根据您要完成的任务,我会建议寻找到FormsAuthentication。这样,您可以简单地指定安全文件夹和一个登录页面和.NET处理踢访问者登录页面,如果他们不进行身份验证。

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");

    }

解决方案

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.

这篇关于检查Session变量和重定向页面加载之前到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆