asp.net 단일 사용자 로그인 클래식 솔루션

5084 단어
      ,   WEB   ,                !

               ,        ,             。

                   ,     1,   0,         ,   1        ,     ,        ,             ,  ,                ,      IE,    ,                ,         ,                :        ,              ,          ,                   ,     。

      ,  .net            。    :

  :  global.asax  , Session_End          :

             Hashtable h = (Hashtable)Application["online"];
             if (h[Session.SessionID] != null)
                 h.Remove(Session.SessionID);
             Application["online"] = h;

  :  web.config  , system.web       

<sessionState mode="InProc"></sessionState>

       global.asax  session_end    。

  :        ,                   ,        ,      。       ,          。

private void isLogin()
     {
         Hashtable h = (Hashtable)Application["online"];

         if (h == null)
         {
             h = new Hashtable();
         }

         //       Application   (    )
         IDictionaryEnumerator e1 = h.GetEnumerator();
         bool flag = false;
         while (e1.MoveNext())
         {
             if (checkCookie(e1.Value.ToString()))
             {
                 flag = true;
                 break;
             }
         }
         if (flag)
         {
             Response.Write("<script defer language='javascript'>alert('This user is online!');history.go(-1);</script>");
         }
         else
         {
             loginLogic login = new loginLogic(this.txt_user_id.Text.Trim(), this.txt_password.Text.Trim());

             if (!login.getLoginStatus)
             {
                 Response.Write("<script defer language='javascript'>alert('Invalid UserID or password.Please try again.');</script>");
             }
             else
             {
                 //        
                 DateTime now = DateTime.Now;
                 string cookieValue = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString();
                 // userid +           
                 h[Session.SessionID] = this.txt_user_id.Text.Trim() + "-" + cookieValue;
                 Application["Online"] = h;
                 //         cookie
                 Response.Cookies["hqs"].Value =cookieValue;
                 Response.Cookies["hqs"].Expires = DateTime.Now.AddDays(1);

                 //      session
                 Session["userid"] = this.txt_user_id.Text.Trim();
                 Response.Redirect("Manage/index.aspx");
             }
         }
     }

private bool checkCookie(string appValue)
     {
         bool isExist = false;

         if (Request.Cookies["hqs"] != null)
         {
             string cookieValue = Request.Cookies["hqs"].Value;

             char[] sp = new char[1]{'-'};
             string appUserid = appValue.Split(sp)[0].ToString();
             string appCookie = appValue.Split(sp)[1].ToString();

             if (appUserid == this.txt_user_id.Text.Trim() && appCookie != cookieValue)
                 isExist = true;
         }
         return isExist;       
     }

  : VS2005   WEB           ,    IIS         .

        :     session  timeout   20  ,    ,             ,       ,    session     ,      global.asax  session_end  ,            ,20           ,           ,          。          IE ,       ,         。

  ,session  timeout        ,20        。    :

<sessionState mode="InProc" timeout="       "></sessionState>

------------------------

   ,        ,           ,session 20       END  ,            ,              。        。            ,      。

  :         ,        

 、       ,                          ,              。 ASP.NET            ,                  。

    

  Cache   ,             Cache ,        Session     ,  ,  Session  ,   Cache   ; Cache           ,  ,                。


    
string sKey = username.Text.ToString().Trim(); //   Cache    Key  
             string sUser = Convert.ToString(Cache[sKey]); //       
             if (sUser == null || sUser == String.Empty)
             {
               
                 TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//  Session     
                 HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);//    cache       
               //    
             }
             else if (Cache[sKey].ToString() == sKey)//          
             {
                 ClientScript.RegisterStartupScript(GetType(), "  ", "<script>alert('   ,        ');</script>");
                 return;
             }
             else
             {
                 Session.Abandon();//                     
             }

좋은 웹페이지 즐겨찾기