문제

i just want to redirect user to Home Page(Default.aspx) when session has been expired in asp.net 3.5. i just do it with web user control but steel it's not work perfectly. so i just want to do it with web.config.

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

Is this technique works in .net 3.5 framework application.

도움이 되었습니까?

해결책 3

If you are using form authentication then you don't have to write any custom code. For session timeout settings are provided by Framework itself. Just change the configuration file as mentioned below :

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

Above configuration will redirect user to the login page when session expires.

다른 팁

For No Master Page:

you may try this.

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

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

Use this logic in every webpage

If Master Page is Used:

Use the above logic in your masterpage.cs file

Using Web.Config:

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

You can check for session on page_init as show below

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

I would use a masterpage for all webforms except the SignIn.aspx and have this in the masterpages init method:

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

MSDN article about forms authentication.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top