source

양식 인증 시간 초과 대 세션 시간 초과

manycodes 2023. 6. 20. 21:43
반응형

양식 인증 시간 초과 대 세션 시간 초과

나의 asp.net 웹사이트에서 나는 다음 구성을 가진 asp.net 양식 인증을 사용하고 있습니다.

<authentication mode="Forms">
    <forms loginUrl="~/Pages/Common/Login.aspx"
           defaultUrl="~/Pages/index.aspx"
           protection="All"
           timeout="30"
           name="MyAuthCookie"
           path="/"
           requireSSL="false"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" >
    </forms>
</authentication>

다음과 같은 질문이 있습니다.

  1. 양식 인증 전에 만료될 세션으로 인해 양식 인증 내부에서 슬라이딩 만료를 사용하고 있으므로 세션의 시간 초과 값이 무엇이어야 합니다.어떻게 보호할 수 있을까요?

  2. 인증 로그아웃 후 logout.aspx에서 페이지를 리디렉션하고 싶지만 loginpage.aspx에서 자동으로 리디렉션됩니다.그게 어떻게 가능해?

  1. 안전한 쪽으로 가기 위해:시간 초과(세션) <= 시간 초과(양식 인증) * 2
  2. 인증 시간 초과 후 loginUrl 특성에 지정되지 않은 페이지를 표시하려면 ASP.NET이 이를 수행하는 방법을 제공하지 않으므로 이를 수동으로 처리해야 합니다.

#2를 달성하기 위해 쿠키와 쿠키의 인증을 수동으로 확인할 수 있습니다.만료 티켓을 발급하고 만료된 경우 사용자 지정 페이지로 리디렉션합니다.
다음 이벤트 중 하나에서 수행할 수 있습니다.요청 상태 획득, 요청 인증.

이벤트의 샘플 코드는 다음과 같습니다.

// Retrieve AuthenticationCookie
var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) return;
FormsAuthenticationTicket ticket = null;
try {
    ticket = FormsAuthentication.Decrypt(cookie.Value);
} catch (Exception decryptError) {
    // Handle properly
}
if (ticket == null) return; // Not authorised
if (ticket.Expiration > DateTime.Now) {
    Response.Redirect("SessionExpiredPage.aspx"); // Or do other stuff here
}

세션 종속성이 있는 사이트의 경우 global.asax에서 session start 이벤트를 사용하여 오래된 인증에서 로그아웃하기만 하면 됩니다.

void Session_Start(object sender, EventArgs e)
{
  if (HttpContext.Current.Request.IsAuthenticated)
  {

    //old authentication, kill it
    FormsAuthentication.SignOut();
    //or use Response.Redirect to go to a different page
    FormsAuthentication.RedirectToLoginPage("Session=Expired");
    HttpContext.Current.Response.End();
  }

}

이렇게 하면 새 세션 = 새 인증, 기간이 지정됩니다.

언급URL : https://stackoverflow.com/questions/1470777/forms-authentication-timeout-vs-session-timeout

반응형