ASP.NET 登入驗證

  • 4648
  • 0

摘要:ASP.NET登入驗證

登入完成之後通常會給個Session,接下來的程式判斷有無指定此Session即可;如果程式很多的話,每支都要判斷?聽起來就很麻煩…… ASP.NET的 Global有個事件會在每支程式啟動時執行:

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
namespace project
{
 	public class Global : System.Web.HttpApplication
	{
  		private System.ComponentModel.IContainer components = null;
		public Global()
  		{
 			InitializeComponent();
  		}
		private void Global_AcquireRequestState(object sender, System.EventArgs e)
		{
			if ((string)Session["ID"]==null)
			{
				Response.Redirect(Request.ApplicationPath +"/login.aspx");
  			}
		}
		private void InitializeComponent()
  		{
		//this.components = new System.ComponentModel.Container();
		this.AcquireRequestState += new System.EventHandler(this.Global_AcquireRequestState);
		}
	}
}

這是 1.1舊時代使用的方法;ASP.NET 2.0似乎有新功能可用

blog.miniasp.com/post/2008/02/Explain-Forms-Authentication-in-ASPNET-20.aspx