簡體   English   中英

如何在asp.net 3.5中將用戶重定向到會話超時的默認頁面

[英]how to redirect user to default page on session time out in asp.net 3.5

我只想在asp.net 3.5中的會話過期時將用戶重定向到主頁(Default.aspx)。 我只是通過網絡用戶控制來實現這一點,但鋼鐵並不能完美運行。 所以我只想用web.config來做。

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

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

對於無主頁面:

你可以試試這個。

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>

您可以在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");
   }
}

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

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

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

我會為除SignIn.aspx之外的所有webforms使用一個SignIn.aspx ,並在masterpages init方法中使用它:

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

關於表單身份驗證的MSDN文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM