(转贴)一套.net窗体身份验证方案(解决了防止用户重复登陆,sessio
http://blog.csdn.net/johnsontj/articles/324369.aspx 一. 设置 web.config 相关选项 先启用窗体身份验证和默认登陆页,如下。 authenticationmode="Forms" formsloginUrl="default.aspx"/forms /authentication 设置网站可以匿名访问,如下 authorization
http://blog.csdn.net/johnsontj/articles/324369.aspx
一. 设置web.config相关选项
先启用窗体身份验证和默认登陆页,如下。
设置网站可以匿名访问,如下
然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。
把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。
设置session超时时间为1分钟,并启用cookieless,如下。
为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。
二. 设置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()方法,方法源码如下
///
/// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用 ///
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目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.
///
/// 防止用户重复登陆,在用户将要身份验证前使用
///
/// 要验证的用户名字
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
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
{
///
/// 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录
///
protected bool IsAdminDir
{
get
{
return Request.FilePath.IndexOf("/oa/admin")==0;
}
}
///
/// 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页
///
private void PreventSessionTimeout()
{
if(!this.IsAdminDir) return;
if(Session["User_Name"]==null&&this.IsAdminDir)
{
System.Web.Security.FormsAuthentication.SignOut();
this.Alert("登陆超时",Request.ApplicationPath)
}
}
///
/// 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用.
///
private void UpdateCacheTime()
{
Hashtable h=(Hashtable)Cache["online"];
if(h!=null)
{
((ArrayList)h[Session.SessionID])[1]=DateTime.Now;
}
Cache["Online"]=h;
}
///
/// 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据
///
///
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++;
}
}
///
/// 弹出信息并返回到指定页
///
/// 弹出的消息
/// 指定转向的页面
protected void Alert(string msg,string url)
{
string scriptString = "<script>alert(/""+msg+"/");location.href=/""+url+"/"</script>";
if(!this.IsStartupScriptRegistered("alert"))
this.RegisterStartupScript("alert", scriptString);
}
///
/// 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求
/// 这个方法也在下面的OnInit方法里调用
///
protected void XmlReLoad()
{
System.Text.StringBuilder htmlstr=new System.Text.StringBuilder();
htmlstr.Append("
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(" ");
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"]);
}
}
}
}
五. 写一个自定义异常类
首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
using System;
namespace oa.cls
{
///
/// MyException 的摘要说明。
///
public class MyException:ApplicationException
{
///
/// 构造函数
///
public MyException():base()
{
}
///
/// 构造函数
///
/// 异常消息
public MyException(string Message):base(Message)
{
System.Web.HttpContext.Current.Response.Redirect("~/ShowErr.aspx?msg="+Message);
}
///
/// 构造函数
///
/// 异常消息
/// 引起该异常的异常类
public MyException(string Message,Exception InnerException):base(Message,InnerException)
{
}
}
}
六.总结
我发现在Session里保存的值,比如session["name"]是没有任何向服务器的请求达到1分钟后就会自动丢失,但是session ID是关闭同一进程的浏览器页面后达1分钟后才会丢失并更换的,因为只要你开着浏览器就会有session ID,无论是在url里保存还是在cookies里。不知道这个结论对不对,反正我在设置了session的timeout为1分钟后,session["name"]的值已经没有了,可是SessionID还是旧的,Global.asax里的Session_End里的代码也没有执行,而身份验证票据也没有丢失。我不知道这三者之间的关系是怎样的,谁先谁后,好像在
以上这些代码比较零散,我花费了2天的时间才总结出来这套方案,不是很完美,但是暂时只能这样了,不能在这方面浪费很多时间了,大家可以把上面的代码组织到一个类里,然后把方法都修改成静态方法方便调用
最后大家有什么建议和改进的意见欢迎和我交流。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to the problem that Win11 system cannot install Chinese language pack With the launch of Windows 11 system, many users began to upgrade their operating system to experience new functions and interfaces. However, some users found that they were unable to install the Chinese language pack after upgrading, which troubled their experience. In this article, we will discuss the reasons why Win11 system cannot install the Chinese language pack and provide some solutions to help users solve this problem. Cause Analysis First, let us analyze the inability of Win11 system to

As smartphone technology continues to develop, mobile phones play an increasingly important role in our daily lives. As a flagship phone focusing on gaming performance, the Black Shark phone is highly favored by players. However, sometimes we also face the situation that the Black Shark phone cannot be turned on. At this time, we need to take some measures to solve this problem. Next, let us share five tips to teach you how to solve the problem of Black Shark phone not turning on: Step 1: Check the battery power. First, make sure your Black Shark phone has enough power. It may be because the phone battery is exhausted

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

Everyone knows that if the computer cannot load the driver, the device may not work properly or interact with the computer correctly. So how do we solve the problem when a prompt box pops up on the computer that the driver cannot be loaded on this device? The editor below will teach you two ways to easily solve the problem. Unable to load the driver on this device Solution 1. Search for "Kernel Isolation" in the Start menu. 2. Turn off Memory Integrity, and it will prompt "Memory Integrity has been turned off. Your device may be vulnerable." Click behind to ignore it, and it will not affect the use. 3. The problem can be solved after restarting the machine.

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

Title: Analysis of Oracle Error 3114: Causes and Solutions When using Oracle database, you often encounter various error codes, among which error 3114 is a relatively common one. This error generally involves database link problems, which may cause exceptions when accessing the database. This article will interpret Oracle error 3114, discuss its causes, and give specific methods to solve the error and related code examples. 1. Definition of error 3114 Oracle error 3114 pass

Are you worried about WordPress backend garbled code? Try these solutions, specific code examples are required. With the widespread application of WordPress in website construction, many users may encounter the problem of garbled code in the WordPress backend. This kind of problem will cause the background management interface to display garbled characters, causing great trouble to users. This article will introduce some common solutions to help users solve the trouble of garbled characters in the WordPress backend. Modify the wp-config.php file and open wp-config.

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are
