一. 设置web.config相关选项
先启用窗体身份验证和默认登陆页,如下。
<authentication mode="Forms">
<forms loginUrl="default.aspx"></forms>
</authentication>
设置网站可以匿名访问,如下
<authorization>
<allow users="*" />
</authorization>
然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。
<location path="admin">
<system.web>
<authorization>
<deny users="?"></deny>
</authorization>
</system.web>
</location>
把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。
<globalization requestEncoding="gb2312" responseEncoding="gb2312" />
设置session超时时间为1分钟,并启用cookieless,如下。
<sessionState mode="InProc" cookieless="true" timeout="1" />
为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。
<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方法里调用LogoutCache()方法,方法源码如下
/// <summary>
/// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用 /// </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;
}
}
三. 设置相关的登陆和注销代码
登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache["online"]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.
/// <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;
}
用户注销的时候调用上面提到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超时,如果超时就注销身份验证并提示和转向到网站默认页
/// </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方法里调用.
/// </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方法里调用.以便方便的观察缓存数据
/// </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("当前的时间和此登陆时间间隔的秒数",ts.TotalSeconds.ToString());
i++;
}
}
/// <summary>
/// 弹出信息并返回到指定页
/// </summary>
/// <param name="msg">弹出的消息</param>
石家庄网站建设的优点:我们是专属定制,制作符合您公司业务、风格的网站,这就做到给您制作的网站是网络上独一无二的网站,这样更有利于搜索引擎的收录。如果您喜欢网络上某个风格的网站或者网站模板,我们是只仿制而不直接套用,我们会对其中的结构和样式做增减优化,做…
竞价排名营销是一种通过 竞价购买搜索引擎中关键词或关键短语的广告方式。竞价排名营销分为两种类型。 第一种,直接为真正的搜索引擎结果的排名付费,也就是说付的钱越多,网页所获得的排名名也就越高。 第二种则更类似于普通的广告。这种竞价排名营销方式通过…
网站优化对网站来说是非常重要的,直接决定和影响着网站所起到的作用,以及在各个地方当中的排名。在进行seo优化的过程中,我们必须要坚持一定的原则,真正的把这些事情做得更好,才可以确保最终的结果,这对于我们来说是非常重要的事。 SEO优化主要分为8小步: …