2014-04-02 43 views
3

我只是想重定向用戶到主頁(Default.aspx)當會話已在asp.net 3.5中過期。 我只是用網絡用戶控制來做,但鋼鐵不是完美的。所以我只想用web.config來做到這一點。如何將用戶重定向到默認頁面會話超時在asp.net 3.5

<authentication mode="Forms"> 
    <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" /> 
</authentication> 

此技術是否適用於.net 3.5框架應用程序。

回答

3

如果您使用表單身份驗證那麼您不必編寫任何自定義代碼。對於會話超時設置由框架本身提供。只需更改配置文件,如下所述:

<authentication mode="Forms"> 
    <forms defaultUrl="~/Default.aspx" 
     loginUrl="~/Login.aspx" 
     slidingExpiration="true" 
     timeout="60" /> 
</authentication> 

上述的配置將在會話過期用戶重定向到登錄頁面。

+0

我可以設置默認和登錄URL相同。 –

+0

它不工作。 –

2

您可以在page_init檢查會話顯示如下

protected void Page_Init(object sender, EventArgs e) 
{ 
    CheckSession(); 
} 

private void CheckSession() 
{ 
    if (Session["SessionID"] == null || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated) 
    { 
     Response.Redirect("Default.aspx"); 
    } 
} 
+0

無法正常工作..... –

1

我會用一個母版的所有web表單除了SignIn.aspx並在masterpages init方法有這樣的:

if((System.Web.HttpContext.Current.User == null) || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated) 
    Response.Redirect("SignIn.aspx"); 

MSDN article about forms authentication.

3

For No Master Page:

你可以試試這個。

protected void Page_Load(object sender, EventArgs e) 
    { 
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) 
    { 
     if (!IsPostBack) 
     { 

     } 
    } 
    else 
    { 
     Response.Redirect("Default.aspx", false); 
    } 
} 

使用這種邏輯在每一個網頁

如果母版頁用於:

使用您masterpage.cs上述邏輯文件

使用的Web.Config:

<authentication mode="Forms"> 
    <forms loginUrl="~/SignIn.aspx" protection="All" timeout="2880" path="/" /> 
</authentication> 
<authorization> 
<deny users="?" /> 
</authorization> 
+0

爲什麼在每個網頁上有asp.net webforms的主頁? http://msdn.microsoft.com/en-us/library/wtxbf3hh.ASPX –

+0

檢查更新的答案 – Ajay

相關問題