asp.net 簡單單點登錄技術分析
更新時間:2011年02月14日 21:58:05 作者:
單點登錄,又叫SSO(Single Sign On)。在一些cms或者OA中比較常用到這種登錄模式,目的是為防止重復登錄。而其實現原理也頗為簡單,只要Cache的形式就可以實現,這里只用于簡單記錄下,呵呵……
代碼如下:
///單點登錄(Single Sign On)
public void SSOMethods(string username, string password)
{
//判斷登錄情況 此處方法省略……
int result = CheckLogin(username, password);
if(result>0)
{
//唯一標識,可自行設定
string key = string.Format("{0}_{1}",username, password);
//得到Cache中的key值
string userCache = Cache[key].ToString();
//判斷是否為空
if(string.IsNullOrEmpty(userCache))
{
TimeSpan SessionTimeOut = new TimeSpan(0,0,HttpContext.Current.Session.TimeOut,0,0);
HttpContext.Current.Cache.Insert(key,key,null,DateTime.MaxValue,SessionTimeOut,CacheItemPriority,NotRemovable,null);
Session["User"] = key;
Response.Write("<font color=red>登錄成功!</font>");
}
else
{
Repsonse.Write("<font color=red>抱歉,您已經在其他地方登錄了!</font>");
return;
}
}
else
{
Response.Write("用戶名不存在!");
}
}
復制代碼 代碼如下:
///單點登錄(Single Sign On)
public void SSOMethods(string username, string password)
{
//判斷登錄情況 此處方法省略……
int result = CheckLogin(username, password);
if(result>0)
{
//唯一標識,可自行設定
string key = string.Format("{0}_{1}",username, password);
//得到Cache中的key值
string userCache = Cache[key].ToString();
//判斷是否為空
if(string.IsNullOrEmpty(userCache))
{
TimeSpan SessionTimeOut = new TimeSpan(0,0,HttpContext.Current.Session.TimeOut,0,0);
HttpContext.Current.Cache.Insert(key,key,null,DateTime.MaxValue,SessionTimeOut,CacheItemPriority,NotRemovable,null);
Session["User"] = key;
Response.Write("<font color=red>登錄成功!</font>");
}
else
{
Repsonse.Write("<font color=red>抱歉,您已經在其他地方登錄了!</font>");
return;
}
}
else
{
Response.Write("用戶名不存在!");
}
}
相關文章
asp.net下通過泛解析和偽靜態(tài)實現二級域名的實現方法
當我們想做一個站群或想為每一個會員的主頁設置為一個二級域名時,總是想拼命的去找些組件來實現。2010-10-10Entity Framework Core延遲加載(懶加載)用法
這篇文章介紹了Entity Framework Core延遲加載(懶加載)的使用方式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02Linux安裝.Net core 環(huán)境并運行項目的方法
這篇文章主要介紹了Linux安裝.Net core 環(huán)境并運行項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08