欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
[ASP.NET(C#)] 防止重復登錄方法

一.設置web.config相關(guān)選項
先啟用窗體身份驗證和默認登陸頁(yè),如下。

<authentication mode="Forms">
<forms loginUrl="default.aspx"></forms>
</authentication>

 

設置網(wǎng)站可以匿名訪(fǎng)問(wèn),如下

<authorization>
<allow users="*" />
</authorization>

 

然后設置跟目錄下的admin目錄拒絕匿名登陸,如下。注意這個(gè)小節在System.Web小節下面。

<location path="admin">
<system.web>
<authorization>
<deny users="?"></deny>
</authorization>
</system.web>
</location>

 

把http請求和發(fā)送的編碼設置成GB2312,否則在取查詢(xún)字符串的時(shí)候會(huì )有問(wèn)題,如下。

<globalization requestEncoding="gb2312" responseEncoding="gb2312" />

 

設置session超時(shí)時(shí)間為1分鐘,并啟用cookieless,如下。

<sessionState mode="InProc" cookieless="true" timeout="1" />

 

為了啟用頁(yè)面跟蹤,我們先啟用每一頁(yè)的trace,以便我們方便的調試,如下。

<trace enabled="true" requestLimit="1000" pageOutput="true" traceMode="SortByTime" localOnly="true" />

 

 

 

 

 

二.設置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方法里調用LogoutCache()方法,方法源碼如下

/// <summary>
/// 清除Cache里當前的用戶(hù),主要在Global.asax的Session_End方法和用戶(hù)注銷(xiā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;
}
}

 

 

 

 

 

三.設置相關(guān)的登陸和注銷(xiāo)代碼

      登陸前調用PreventRepeatLogin()方法,這個(gè)方法可以防止用戶(hù)重復登陸,如果上次用戶(hù)登陸超時(shí)大于1分鐘,也就是關(guān)閉了所有admin目錄下的頁(yè)面達到60秒以上,就認為上次登陸的用戶(hù)超時(shí),你就可以登陸了,如果不超過(guò)60秒,就會(huì )生成一個(gè)自定義異常。在Cache["online"]里保存了一個(gè)哈西表,哈西表的key是當前登陸用戶(hù)的SessionID,而Value是一個(gè)ArrayList,這個(gè)ArrayList有兩個(gè)元素,第一個(gè)是用戶(hù)登陸的名字第二個(gè)元素是用戶(hù)登陸的時(shí)間,然后在每個(gè)admin目錄下的頁(yè)刷新頁(yè)面的時(shí)候會(huì )更新當前登陸用戶(hù)的登陸時(shí)間,而只admin目錄下有一個(gè)頁(yè)打開(kāi)著(zhù),即使不手工向服務(wù)器發(fā)送請求,也會(huì )自動(dòng)發(fā)送一個(gè)請求更新登陸時(shí)間,下面我在頁(yè)面基類(lèi)里寫(xiě)了一個(gè)函數來(lái)做到這一點(diǎn),其實(shí)這會(huì )增加服務(wù)器的負擔,但在一定情況下也是一個(gè)可行的辦法.

/// <summary>
/// 防止用戶(hù)重復登陸,在用戶(hù)將要身份驗證前使用
/// </summary>
/// <param name="name">要驗證的用戶(hù)名字</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("對不起,你輸入的賬戶(hù)正在被使用中,如果你是這個(gè)賬戶(hù)的真正主人,請在下次登陸時(shí)及時(shí)的更改你的密碼,因為你的密碼極有可能被盜竊了!");
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;
}

 

用戶(hù)注銷(xiāo)的時(shí)候調用上面提到LogoutCache()方法

 

 

 

 

 

四.設置admin目錄下的的所有頁(yè)面的基類(lèi)

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>
/// 獲取本頁(yè)是否在受保護目錄,我這里整個(gè)程序在OA的虛擬目錄下,受保護的目錄是admin目錄
/// </summary>
protected bool IsAdminDir
{
get
{
return Request.FilePath.IndexOf("/oa/admin") == 0;
}
}
/// <summary>
/// 防止session超時(shí),如果超時(shí)就注銷(xiāo)身份驗證并提示和轉向到網(wǎng)站默認頁(yè)
/// </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>
/// 每次刷新本頁(yè)面的時(shí)候更新Cache里的登陸時(shí)間選項,在下面的OnInit方法里調用.
/// </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方法里調用.以便方便的觀(guān)察緩存數據
/// </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í)間和此登陸時(shí)間間隔的秒數", ts.TotalSeconds.ToString());
i
++;
}
}
/// <summary>
/// 彈出信息并返回到指定頁(yè)
/// </summary>
/// <param name="msg">彈出的消息</param>
/// <param name="url">指定轉向的頁(yè)面</param>
protected void Alert(string msg, string url)
{
string scriptString = "<script language=JavaScript>alert(\"" + msg + "\");location.href=\"" + url + "\"</script>";
if (!this.IsStartupScriptRegistered("alert"))
this.RegisterStartupScript("alert", scriptString);
}
/// <summary>
/// 為了防止常時(shí)間不刷新頁(yè)面造成會(huì )話(huà)超時(shí),這里寫(xiě)一段腳本,每隔一分鐘向本頁(yè)發(fā)送一個(gè)請求以維持會(huì )話(huà)不被超時(shí),這里用的是xmlhttp的無(wú)刷新請求
/// 這個(gè)方法也在下面的OnInit方法里調用
/// </summary>
protected void XmlReLoad()
{
System.Text.StringBuilder htmlstr
= new System.Text.StringBuilder();
htmlstr.Append(
"<SCRIPT LANGUAGE=\"JavaScript\">");
htmlstr.Append(
"function GetMessage(){");
htmlstr.Append(
" var xh=new ActiveXObject(\"Microsoft.XMLHTTP\");");
htmlstr.Append(
" xh.open(\"get\",window.location,false);");
htmlstr.Append(
" xh.send();");
htmlstr.Append(
" window.setTimeout(\"GetMessage()\",60000);");
htmlstr.Append(
"}");
htmlstr.Append(
"window.onload=GetMessage();");
htmlstr.Append(
"</SCRIPT> ");
if (!this.IsStartupScriptRegistered("xmlreload"))
this.RegisterStartupScript("alert", htmlstr.ToString());
}
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
this.PreventSessionTimeout();
this.UpdateCacheTime();
this.XmlReLoad();
if (this.Cache["online"] != null)
{
this.TraceValues((System.Collections.Hashtable)Cache["online"]);
}
}
}
}

 

 

 

 

 

 

五.寫(xiě)一個(gè)自定義異常類(lèi)
首先要在跟目錄下寫(xiě)一個(gè)錯誤顯示頁(yè)面ShowErr.aspx,這個(gè)頁(yè)面根據傳遞過(guò)來(lái)的查詢(xún)字符串msg的值,在一個(gè)Label上顯示錯誤信息。

using System;
namespace oa.cls
{
/// <summary>
/// MyException 的摘要說(shuō)明。
/// </summary>
public class MyException : ApplicationException
{
/// <summary>
/// 構造函數
/// </summary>
public MyException()
:
base()
{
}
/// <summary>
/// 構造函數
/// </summary>
/// <param name="ErrMessage">異常消息</param>
public MyException(string Message)
:
base(Message)
{
System.Web.HttpContext.Current.Response.Redirect(
"~/ShowErr.aspx?msg=" + Message);
}
/// <summary>
/// 構造函數
/// </summary>
/// <param name="Message">異常消息</param>
/// <param name="InnerException">引起該異常的異常類(lèi)</param>
public MyException(string Message, Exception InnerException)
:
base(Message, InnerException)
{
}
}
}
分類(lèi)
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
asp.net錯誤處理封裝
C# 數據緩存操作Cache
網(wǎng)站全局緩存機制(類(lèi)),可自動(dòng)定時(shí)清理 --- 需觸發(fā) - .NET 3.x - 小組 -...
Discuz!NT中集成Memcached分布式緩存
一步一步Asp.Net MVC系列_權限管理總結(附MVC權限管理系統源碼)
微信公眾平臺開(kāi)發(fā)教程(四) 實(shí)例入門(mén):機器人(附源碼)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久