服務(wù)熱線
153 8323 9821
一. 設(shè)置web.config相關(guān)選項(xiàng)
先啟用窗體身份驗(yàn)證和默認(rèn)登陸頁,如下。
<authentication mode="Forms">
<forms loginUrl="default.aspx"></forms>
</authentication>
設(shè)置網(wǎng)站可以匿名訪問,如下
<authorization>
<allow users="*" />
</authorization>
然后設(shè)置跟目錄下的admin目錄拒絕匿名登陸,如下。注意這個(gè)小節(jié)在System.Web小節(jié)下面。
<location path="admin">
<system.web>
<authorization>
<deny users="?"></deny>
</authorization>
</system.web>
</location>
把http請(qǐng)求和發(fā)送的編碼設(shè)置成GB2312,否則在取查詢字符串的時(shí)候會(huì)有問題,如下。
<globalization requestEncoding="gb2312" responseEncoding="gb2312" />
設(shè)置session超時(shí)時(shí)間為1分鐘,并啟用cookieless,如下。
<sessionState mode="InProc" cookieless="true" timeout="1" />
為了啟用頁面跟蹤,我們先啟用每一頁的trace,以便我們方便的調(diào)試,如下。
<trace enabled="true" requestLimit="1000" pageOutput="true" traceMode="SortByTime" localOnly="true" />
二. 設(shè)置Global.asax文件
處理Application_Start方法,實(shí)例化一個(gè)哈西表,然后保存在Cache里
protected void Application_Start(Object sender, EventArgs e)
{
Hashtable h=new Hashtable();
Context.Cache.Insert("online",h);
}
在Session_End方法里調(diào)用LogoutCache()方法,方法源碼如下
/// <summary>
/// 清除Cache里當(dāng)前的用戶,主要在Global.asax的Session_End方法和用戶注銷的方法里調(diào)用 /// </summary>
public void LogoutCache()
{
Hashtable h=(Hashtable)Context.Cache["online"];
if(h!=null)
{
if(h[Session.SessionID]!=null)
h.Remove(Session.SessionID);
Context.Cache["online"]=h;
}
}
三. 設(shè)置相關(guān)的登陸和注銷代碼
登陸前調(diào)用PreventRepeatLogin()方法,這個(gè)方法可以防止用戶重復(fù)登陸,如果上次用戶登陸超時(shí)大于1分鐘,也就是關(guān)閉了所有admin目錄下的頁面達(dá)到60秒以上,就認(rèn)為上次登陸的用戶超時(shí),你就可以登陸了,如果不超過60秒,就會(huì)生成一個(gè)自定義異常。在Cache["online"]里保存了一個(gè)哈西表,哈西表的key是當(dāng)前登陸用戶的SessionID,而Value是一個(gè)ArrayList,這個(gè)ArrayList有兩個(gè)元素,第一個(gè)是用戶登陸的名字第二個(gè)元素是用戶登陸的時(shí)間,然后在每個(gè)admin目錄下的頁刷新頁面的時(shí)候會(huì)更新當(dāng)前登陸用戶的登陸時(shí)間,而只admin目錄下有一個(gè)頁打開著,即使不手工向服務(wù)器發(fā)送請(qǐng)求,也會(huì)自動(dòng)發(fā)送一個(gè)請(qǐng)求更新登陸時(shí)間,下面我在頁面基類里寫了一個(gè)函數(shù)來做到這一點(diǎn),其實(shí)這會(huì)增加服務(wù)器的負(fù)擔(dān),但在一定情況下也是一個(gè)可行的辦法.
/// <summary>
/// 防止用戶重復(fù)登陸,在用戶將要身份驗(yàn)證前使用
/// </summary>
/// <param name="name">要驗(yàn)證的用戶名字</param>
private void PreventRepeatLogin(string name)
{
Hashtable h=(Hashtable)Cache["online"];
if(h!=null)
{
IDictionaryEnumerator e1=h.GetEnumerator();
bool flag=false;
while(e1.MoveNext())
{
if((string)((ArrayList)e1.Value)[0]==name)
{
flag=true;
break;
}
}
if(flag)
{
TimeSpan ts=System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[1]));
if(ts.TotalSeconds<60)
throw new oa.cls.MyException("對(duì)不起,你輸入的賬戶正在被使用中,如果你是這個(gè)賬戶的真正主人,請(qǐng)?jiān)谙麓蔚顷憰r(shí)及時(shí)的更改你的密碼,因?yàn)槟愕拿艽a極有可能被盜竊了!");
else
h.Remove(e1.Key);
}
}
else
{
h=new Hashtable();
}
ArrayList al=new ArrayList();
al.Add(name);
al.Add(System.DateTime.Now);
h[Session.SessionID]=al;
if(Cache["online"]==null)
{
Context.Cache.Insert("online",h);
}else
Cache["Online"]=h;
}
用戶注銷的時(shí)候調(diào)用上面提到LogoutCache()方法
四. 設(shè)置admin目錄下的的所有頁面的基類
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
namespace oa.cls
{
public class MyBasePage : System.Web.UI.Page
{
/// <summary>
/// 獲取本頁是否在受保護(hù)目錄,我這里整個(gè)程序在OA的虛擬目錄下,受保護(hù)的目錄是admin目錄
/// </summary>
protected bool IsAdminDir
{
get
{
return Request.FilePath.IndexOf("/oa/admin")==0;
}
}
/// <summary>
/// 防止session超時(shí),如果超時(shí)就注銷身份驗(yàn)證并提示和轉(zhuǎn)向到網(wǎng)站默認(rèn)頁
/// </summary>
private void PreventSessionTimeout()
{
if(!this.IsAdminDir) return;
if(Session["User_Name"]==null&&this.IsAdminDir)
{
System.Web.Security.FormsAuthentication.SignOut();
this.Alert("登陸超時(shí)",Request.ApplicationPath)
}
}
/// <summary>
/// 每次刷新本頁面的時(shí)候更新Cache里的登陸時(shí)間選項(xiàng),在下面的OnInit方法里調(diào)用.
/// </summary>
private void UpdateCacheTime()
{
Hashtable h=(Hashtable)Cache["online"];
if(h!=null)
{
((ArrayList)h[Session.SessionID])[1]=DateTime.Now;
}
Cache["Online"]=h;
}
/// <summary>
/// 在跟蹤里輸出一個(gè)HashTable的所有元素,在下面的OnInit方法里調(diào)用.以便方便的觀察緩存數(shù)據(jù)
/// </summary>
/// <param name="myList"></param>
private void TraceValues( Hashtable myList)
{
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
int i=0;
while ( myEnumerator.MoveNext() )
{
Context.Trace.Write( "onlineSessionID"+i, myEnumerator.Key.ToString());
ArrayList al=(ArrayList)myEnumerator.Value;
Context.Trace.Write( "onlineName"+i, al[0].ToString());
Context.Trace.Write( "onlineTime"+i,al[1].ToString());
TimeSpan ts=System.DateTime.Now.Subtract(Convert.ToDateTime(al[1].ToString()));
Context.Trace.Write("當(dāng)前的時(shí)間和此登陸時(shí)間間隔的秒數(shù)",ts.TotalSeconds.ToString());
i++;
}
}
/// <summary>
/// 彈出信息并返回到指定頁
/// </summary>
/// <param name="msg">彈出的消息</param>