본문 바로가기

프로그래밍/.NET

[ASP.NET] web.config 세션 시간 초과 설정

반응형


Web.config에 세션 타임아웃을 늘려보자

 


<system.web> <authentication mode="Forms"> <forms name=".ASPAUTH" loginUrl="~/AppLogin" cookieless="UseCookies" timeout="2880" protection="All" defaultUrl="/AppMain/Index" path="/" /> </authentication> </system.web>

 

다음과 같이 timeout이 2880분으로 설정되어 있다.

즉, 서버와 클라이언트 사이에 2880분 간 연결을 유지한다는 의미다.

 

일반적으로 로그인을 할 경우 다음과 같이 로그인 정보를 가지고 폼 인증 및 쿠키에 해당 정보를 저장하게 된다.

 

<script type="text/javascript" language="javascript"> function Go_Login() { ... this.theFrm.submit(); ... } </script> <form id="theFrm" name="theFrm" action="@ViewBag.DomainPath/AppLogin/LogIn" method="post"> <ul> <li><label for="input_id">아이디</label><input type="text" id="txt_UserID" name="txt_UserID"/></li> <li><label for="input_pw">비밀번호</label><input type="password" id="pas_PassWord" name="pas_PassWord"/></li> </ul> <button type="submit" class="btn_login" onclick="javascript:Go_Login(); return false;">로그인</button> <input type="checkbox" id="chk_login" name="chk_login"/><label for="chk_login">로그인 유지</label> </div>

public ActionResult LogIn(FormCollection collection) { //로그인 정보 string strUserID = collection["txt_UserID"].ToString(); bool boolKeepLogin = collection["chk_login"] != null ? true : false; //Forms 인증처리 FormsAuthentication.RedirectFromLoginPage(strUserID, false); //쿠키 정보 저장 cls_Cookie.SetCookie("UserID", strUserID); ... } public class cls_Cookie { public static void SetCookie(string key, string value) { key = HttpContext.Current.Server.UrlEncode(key); value = HttpContext.Current.Server.UrlEncode(value); HttpCookie cookie = new HttpCookie(key, value); cookie.Expires = DateTime.Now.AddDays(365); SetCookie(cookie); } }


로그인을 하게 되면 로그인 데이터는 쿠키에 365일 간 담고 있지만,

세션이 2880분 (2일)로 세팅되어 있기 때문에 2일 뒤 자동으로 로그아웃이 된다.

 

로그인 당시 로그인유지 체크박스를 통해 로그인 상태를 더 긴 기간으로 설정하고 싶다면,

특정 경우 세션 타임아웃을 증가 시켜야 한다.

 

public ActionResult LogIn(FormCollection collection) { //로그인 정보 string strUserID = collection["txt_UserID"].ToString(); bool boolKeepLogin = collection["chk_login"] != null ? true : false; //Forms 인증처리 FormsAuthentication.RedirectFromLoginPage(strUserID, false); //로그인 유지 (timeOut 7일 증가) if (boolKeepLogin) { FormsAuthenticationTicket tkt; string cookiestr; System.Web.HttpCookie ck; tkt = new FormsAuthenticationTicket(1, strUserID, DateTime.Now, DateTime.Now.AddDays(7), false, strUserID); cookiestr = FormsAuthentication.Encrypt(tkt); ck = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, cookiestr); ck.Path = FormsAuthentication.FormsCookiePath; System.Web.HttpContext.Current.Response.Cookies.Add(ck); } //쿠키 정보 저장 cls_Cookie.SetCookie("UserID", strUserID); ... }




반응형