由于某些原因,在我們的應(yīng)用中會(huì)遇到一個(gè)用戶只能在一個(gè)地方登錄的情況,也就是我們通常所說的單點(diǎn)登錄。在ASP.NET中實(shí)現(xiàn)單點(diǎn)登錄其實(shí)很簡單,下面就把主要的方法和全部代碼進(jìn)行分析。
實(shí)現(xiàn)思路
利用Cache的功能,我們把用戶的登錄信息保存在Cache中,并設(shè)置過期時(shí)間為session失效的時(shí)間,因此,一旦Session失效,我們的Cache也過期;而Cache對所有的用戶都可以訪問,因此,用它保存用戶信息比數(shù)據(jù)庫來得方便。
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的過期時(shí)間 HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPRiority.NotRemovable, null);//將值放入cache己方便單點(diǎn)登錄 //成功登錄 } else if (Cache[sKey].ToString() == sKey)//如果這個(gè)賬號(hào)已經(jīng)登錄 { ClientScr
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注