2013-03-27 73 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"); 
    } 

第二個辦法:你可以在母版頁相同的會話管理。頁面加載事件。

希望這會有所幫助。