ASP.NETワンポイントログイン(コード)

1290 ワード

いくつかの理由で、私たちのアプリケーションでは、1人のユーザーが1つの場所でしかログインできない場合、つまり、私たちが一般的に言っている単一のログインに遭遇します.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();//                     

            }