2013-03-27 45 views
6

我对.NET很新颖 - 我正在制作一个网站,其管理部分应该只对登录用户可见。 我已经创建了登录代码,并且一旦用户通过了身份验证,我就为他们分配了一个会话变量。 我的问题是:是否有更有效的方法来检查会话变量,而不是在每个页面上具有以下功能?检查每个页面中的会话?

protected void Page_Load(object sender, EventArgs e) 
{ 
     checkSession(); 

} 
public void checkSession() 
{ 
    if (Session["LoggedIn"] != "true") 
    { 
     Response.Redirect("default.aspx"); 
    } 
} 

感谢好心!

+0

,你可以在一个类中,这反过来又可以调用cls.checkSession(在checkSession()函数),并返回一个布尔值; – Csharp 2013-03-27 15:24:19

+0

你需要坚持与使用'形式Authentication''cookie' – 2013-03-27 15:24:39

+0

为什么“真”?用true代替。 – 2013-03-27 15:55:17

回答

5

,如果你使用的是MasterPage你可以把检查代码中MasterPage's Page_Load事件,如果不是请使用Global.asax或自定义HttpModule并把cheking代码,在该AcquireRequestState事件处理中第一和PostRequestHandlerExecute事件处理程序用于第二

〔实施例与Global.asax中

public class Global : System.Web.HttpApplication 
{ ... 
    void Application_AcquireRequestState(object sender, EventArgs e) 
    {    
     HttpContext context = HttpContext.Current; 
     // CheckSession() inlined 
     if (context.Session["LoggedIn"] != "true") 
     { 
      context.Response.Redirect("default.aspx"); 
     } 
    } 
    ... 
} 
+0

谢谢!我认为这对我来说是最好的解决方案。 – Dashsa 2013-03-27 16:33:23

+1

ERR_TOO_MANY_REDIRECTS – Elshan 2015-09-29 06:31:57

+0

当重定向到Default.aspx的,还没有登录,从而再次重定向到Default.aspx的 – Snote 2016-05-03 15:55:16

2

派生您的网页从加入您的会话校验码

从页

重写Load方法派生的自定义类现在所有的页面都验证

public class MyPage : System.Web.UI.Page 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Session["yoursession"] != "true") 
    { 
    //code 
    } 
} 



public class yourCustomPage1 : MyPage 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    //you don't have to check or call any method.. 
} 
} 

public class yourCustomPage2 : MyPage 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    //you don't have to check or call any method.. 
} 
} 

等。 。

+1

有一个代码示例说明这一点会很有用。 – 2013-03-27 15:23:50

+0

我正在研究这个 – RollRoll 2013-03-27 15:24:26

1

您可以让您的页面成为继承自检查登录用户的基类的类。

1

开始理解ASP.Net中的表单身份验证的好方法是创建一个全新的网站。 进入Visual Studio并创建新建项目,选择Web,然后选择ASP.NET Web Application。 在账户文件夹中查看它了解过程和ASP.Net方法。

0

您可以创建BasePage并从此基本页面继承所有页面,并将该功能设置在您的基页中。

public class BasePage : Page 
{ 
    protected void checkSession() 
    { 
    if (Session["LoggedIn"] != "true") 
    { 
     Response.Redirect("default.aspx"); 
    } 
    } 
} 

你的页面

public partial class YourPage : BasePage 
{ 
.... 
} 

另一种解决方案:

在你的HTTP模块,创建主体(角色)和身份,以设置认证和授权functionnalities,在您的HTTP模块重视论文信息到当前线程。

链接:http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.isauthenticated.aspx

0
  public class BasePage : Page 
      { 
      protected void checkSession() 
       { 
        if (Session["LoggedIn"] == null) 
        { 
          Response.Redirect("~/default.aspx/"); 
        } 
       } 
      } 
0

第一种方式:Global.asax。CS ADD

void Application_AcquireRequestState(object sender, EventArgs e) 
    { 
     HttpContext context = HttpContext.Current; 
     Page page = context.Handler as Page; 

     if ((string)context.Session["LoggedIn"] != "true" 
      && !(page.AppRelativeVirtualPath == "~/Default.aspx")) 
      context.Response.Redirect("default.aspx"); 
    } 

第二个办法:你可以在母版页相同的会话管理。页面加载事件。

希望这会有所帮助。