ASP.NET單點登入(代碼)

來源:互聯網
上載者:User

標籤:insert   gis   name   思路   bsp   原因   http   錯誤   type   

由於某些原因,在我們的應用中會遇到一個使用者只能在一個地方登入的情況,也就是我們通常所說的單點登入。在ASP.NET中實現單點登入其實很簡單,下面就把主要的方法和全部代碼進行分析。[/p][p=25, null, left]實現思路[/p][p=25, null, left]利用Cache的功能,我們把使用者的登入資訊儲存在Cache中,並設定到期時間為Session失效的時間,因此,一旦Session失效,我們的Cache也到期;而Cache對所有的使用者都可以訪問,因此,用它儲存使用者資訊比資料庫來得方便。[/p]
[p=25, null, left]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();//這段主要是為了避免不必要的錯誤導致不能登入

            }

 

ASP.NET單點登入(代碼)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.