一.設置web.config相關選項
先啟用窗體身份驗證和默認登陸頁,如下。
<authentication mode="Forms">
<forms loginUrl="default.aspx"></forms>
</authentication>
設置網(wǎng)站可以匿名訪問,如下
<authorization>
<allow users="*" />
</authorization>
然后設置跟目錄下的admin目錄拒絕匿名登陸,如下。注意這個小節(jié)在System.Web小節(jié)下面。
<location path="admin">
<system.web>
<authorization>
<deny users="?"></deny>
</authorization>
</system.web>
</location>
把http請求和發(fā)送的編碼設置成GB2312,否則在取查詢字符串的時候會有問題,如下。
<globalization requestEncoding="gb2312" responseEncoding="gb2312" />
設置session超時時間為1分鐘,并啟用cookieless,如下。
<sessionState mode="InProc" cookieless="true" timeout="1" />
為了啟用頁面跟蹤,我們先啟用每一頁的trace,以便我們方便的調(diào)試,如下。
<trace enabled="true" requestLimit="1000" pageOutput="true" traceMode="SortByTime" localOnly="true" />
二.設置Global.asax文件
處理Application_Start方法,實例化一個哈西表,然后保存在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里當前的用戶,主要在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;
}
}
三.設置相關的登陸和注銷代碼
登陸前調(diào)用PreventRepeatLogin()方法,這個方法可以防止用戶重復登陸,如果上次用戶登陸超時大于1分鐘,也就是關閉了所有 admin目錄下的頁面達到60秒以上,就認為上次登陸的用戶超時,你就可以登陸了,如果不超過60秒,就會生成一個自定義異常。在Cache ["online"]里保存了一個哈西表,哈西表的key是當前登陸用戶的SessionID,而Value是一個ArrayList,這個 ArrayList有兩個元素,第一個是用戶登陸的名字第二個元素是用戶登陸的時間,然后在每個admin目錄下的頁刷新頁面的時候會更新當前登陸用戶的登陸時間,而只admin目錄下有一個頁打開著,即使不手工向服務器發(fā)送請求,也會自動發(fā)送一個請求更新登陸時間,下面我在頁面基類里寫了一個函數(shù)來做到這一點,其實這會增加服務器的負擔,但在一定情況下也是一個可行的辦法.
/// <summary>
/// 防止用戶重復登陸,在用戶將要身份驗證前使用
/// </summary>
/// <param name="name">要驗證的用戶名字</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("對不起,你輸入的賬戶正在被使用中,如果你是這個賬戶的真正主人,請在下次登陸時及時的更改你的密碼,因為你的密碼極有可能被盜竊了!");
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;
}
用戶注銷的時候調(diào)用上面提到LogoutCache()方法
四.設置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>
/// 獲取本頁是否在受保護目錄,我這里整個程序在OA的虛擬目錄下,受保護的目錄是admin目錄
/// </summary>
protected bool IsAdminDir
{
get
{
return Request.FilePath.IndexOf("/oa/admin")==0;
}
}
/// <summary>
/// 防止session超時,如果超時就注銷身份驗證并提示和轉(zhuǎn)向到網(wǎng)站默認頁
/// </summary>
private void PreventSessionTimeout()
{
if(!this.IsAdminDir) return;
if(Session["User_Name"]==null&&this.IsAdminDir)
{
System.Web.Security.FormsAuthentication.SignOut();
this.Alert("登陸超時",Request.ApplicationPath)
}
}
/// <summary>
/// 每次刷新本頁面的時候更新Cache里的登陸時間選項,在下面的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>
/// 在跟蹤里輸出一個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("當前的時間和此登陸時間間隔的秒數(shù)",ts.TotalSeconds.ToString());
i++;
}
}
/// <summary>
/// 彈出信息并返回到指定頁
/// </summary>
/// <param name="msg">彈出的消息</param>
/// <param name="url">指定轉(zhuǎn)向的頁面</param>
protected void Alert(string msg,string url)
{
string scriptString = "<script language=JavaScript&g