public class ChkLoginPage:Page
{
public ChkLoginPage()
{
if (uObj.UserIsLogin()==false)
{
Response.Redirect("index.aspx");
return;
}
}
}异常详细信息: System.Web.HttpException: 响应在此上下文中不可用。
{
public ChkLoginPage()
{
if (uObj.UserIsLogin()==false)
{
Response.Redirect("index.aspx");
return;
}
}
}异常详细信息: System.Web.HttpException: 响应在此上下文中不可用。
if (uObj.UserIsLogin()==false)
{
Response.Redirect("index.aspx");
return;
}
}
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
this.Load+=new EventHandler(WaffiePage_Load);
}给你一点代码看看
最好使用forms验证~~~~~~~~要不你就重写init吧
============================================================
用forms验证怎么写,能给点资料看看吗?
写了个例子你看看
基础页BasePage后台加
public void ChkLogin()
{
if (Session["User"] == null)
{
Response.Redirect("Temp.aspx");
}
}
继承页SubPage后台
public class SubPage : BasePageoverride protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
base.ChkLogin();//调用基础页的验证
InitializeComponent();
base.OnInit(e);
}
以上代码经过测试的
public class BasePage:Page
{
public Role rObj = new Role();
public Classes clObj=new Classes();
public Crew crObj=new Crew();
public Detial dtObj=new Detial();
public Options opObj=new Options();
public wjItem wjObj=new wjItem();
public Term tObj=new Term();
public WJPJ.DB.User uObj=new WJPJ.DB.User();
public void showMsg(string msg)
{
showMsg(msg,64);
}
public void showMsg(string msg,int infotype)
{
Response.Write("<script language=vbs>msgbox \""+msg+"\","+infotype+"</script>");
}
public void GoUrl(string url)
{
Response.Write("<script language=vbs>document.url=\""+url+"\"</script>");
}
public void Refresh()
{
Response.Write("<script language=vbs>document.url=document.url</script>");
}
public string getNowFile()
{
return "";
}
} public class ChkLoginPage:BasePage
{
override protected void OnInit(EventArgs e)
{
if (uObj.UserIsLogin()==false)
{
Response.Redirect("index.aspx");
return;
} }
}其他页这样继承就可以了,如果用户没有登陆就会自动跳转到index.aspx。
public class wj:ChkLoginPage
{ }
protected override void OnLoad(EventArgs e)
{}
{
//你的代码
base.OnLoad (e);
}
{
public BasePage()
{
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
base.OnInit(e);
//验证当前用户是否有操作本页的权限
check();}
继承页 public class MainIndex : BasePage
{
/// <summary>
/// 页面基类
/// </summary>
public class PageBase : System.Web.UI.Page
{
#region 构造函数
/// <summary>
/// 空构造函数
/// </summary>
public PageBase()
{
}
#endregion #region 属性 /// <summary>
/// 登录ID
/// </summary>
// public string Loginid = ""; /// <summary>
/// 员工号:唯一标识当前用户
/// </summary>
public string Empid = "-1"; /// <summary>
/// 当前员工姓名
/// </summary>
public string EmpName=""; /// <summary>
/// 功能名称
/// </summary>
public string FunName=""; /// <summary>
/// 窗体功能代码
/// </summary>
public string FunctionName
{
get
{
return GetViewState("FunctionName");
}
set
{
SetViewState("FunctionName",value);
}
} /// <summary>
/// 权限代码
/// </summary>
public int EmpRightCode
{
get
{
try
{
return CheckAuth();
}
catch(NullReferenceException)
{
return -1;
}
}
}
#endregion #region 方法 /// <summary>
/// 从ViewState中获取某个属性的值。如果该属性不存在,返回空字符串。
/// </summary>
/// <param name="PropertyName">属性名称</param>
/// <returns>属性值(属性不存在时返回空字符串)</returns>
public string GetViewState(string PropertyName)
{
try
{
return ViewState[PropertyName].ToString() ;
}
catch(NullReferenceException)
{
return "";
}
} /// <summary>
/// 设置ViewState中某个属性的值
/// </summary>
/// <param name="PropertyName">属性名称</param>
/// <param name="PropertyValue">属性值</param>
public void SetViewState(string PropertyName, string PropertyValue)
{
ViewState[PropertyName]=PropertyValue;
} /// <summary>
/// 权限检验函数
/// </summary>
/// <returns>int</returns>
public int CheckAuth()
{
int _empid = Convert.ToInt16(this.Empid);
string _funcname = this.FunName;
CheckEmpRight myCheckAuth_DA = new CheckEmpRight();
return myCheckAuth_DA.GetEmpRight(_empid,_funcname);
} /// <summary>
/// 给定模块名称,权限检验函数
/// </summary>
/// <returns>int</returns>
public int CheckAuth(string funcname)
{
int _empid = Convert.ToInt16(this.Empid);
CheckEmpRight myCheckAuth_DA = new CheckEmpRight();
return myCheckAuth_DA.GetEmpRight(_empid,funcname);
} /// <summary>
/// 页面初始函数
/// </summary>
/// <param name="fName">模块名称</param>
/// <param name="isChecked">是否验证</param>
public void PageBegin(string fName,bool isChecked)
{
//--------------------------------------------
//设定页面入口的模块名称。没有为空。
//如果本页不定义,一定要从前一页确定。
if (ValidateUtil.isBlank(fName))
{
this.FunctionName=this.GetParaValue("FunctionName");
this.FunName =this.FunctionName;
}
else
{
this.FunName =fName ;
this.FunctionName =fName;
}
//---------------------------------------------
//读取当前用户
//第一步:获取用户唯一标识Empid
this.Empid=this.GetParaValue("EmpID");
this.EmpName =this.GetParaValue("username");
// this.Loginid=this.GetParaValue("LoginID"); //如果Empid为空,表示没有当前用户,则跳转至登录页面。
if (ValidateUtil.isBlank(this.Empid))
{
Response.Redirect(Application["vRoot"]+"/login.aspx");
} //-----------------------------------------------------
//第二步:判断是否具有本页访问权限
if (isChecked && this.CheckAuth() == -1)
{
Response.Redirect(Application["vRoot"]+"/login.aspx");
}
} /// <summary>
/// 记录操作日志
/// </summary>
/// <param name="operation">操作描述</param>
public void WriteOptLog(string operation)
{
//创建登录信息数据结构
LogData mylogdata = new LogData();
//调用数据访问层操作日志服务
LogDB LogDataAccess = new LogDB(); //填写操作日志信息
mylogdata.OperatorID = this.Empid;
mylogdata.OperatorName = this.EmpName;
mylogdata.OperateTime = DateTime.Now.ToString();
mylogdata.FuncName = this.FunName;
mylogdata.OperationDescription = operation; //记录操作日志
LogDataAccess.WriteOperationLog(mylogdata);
} /// <summary>
/// 获取页面参数的值
/// </summary>
/// <param name="oSession">Page.Session</param>
/// <param name="oRequest">Page.Request</param>
/// <param name="paraName">参数名称</param>
/// <returns>参数值,字符串形式</returns>
public string GetParaValue(HttpSessionState oSession, HttpRequest oRequest,string paraName)
{
string strValue="";
//如果Session中能够找到相应的参数值,则返回参数值
try
{
strValue=oSession[paraName].ToString();
}
catch(NullReferenceException)
{
strValue="";
}
//如果Session中不存在,从Request中寻找。
if (ValidateUtil.isBlank(strValue))
{
try
{
strValue=oRequest[paraName].ToString();
}
catch(NullReferenceException)
{
strValue="";
}
//均为空时,返回空字符串。
if (ValidateUtil.isBlank(strValue))
strValue="";
} return strValue;
} /// <summary>
/// 获取页面参数的值
/// </summary>
/// <param name="paraName">参数名称</param>
/// <returns>参数值,字符串形式</returns>
public string GetParaValue(string paraName)
{
return GetParaValue(Session,Request,paraName);
}
#endregion #region 事件处理
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.PageBase_Load);
this.Error += new System.EventHandler(this.PageBase_Error);
} protected void LogEvent(string message, EventLogEntryType entryType)
{
//EventLog提供与Windows事件日志的交互
//如果事件源HipHot没有在计算机中注册,则创建事件源
if (!EventLog.SourceExists("HipHot"))
{
EventLog.CreateEventSource("HipHot", "Application");
}
//将错误日志信息写入Windows事件日志中
EventLog.WriteEntry("HipHot", message, entryType);
}
protected void PageBase_Error(object sender, System.EventArgs e)
{
string errMsg;
//得到系统上一个异常
Exception currentError = Server.GetLastError(); errMsg = "<link rel=\"stylesheet\" href=\"/default.css\">";
errMsg += "<h1>页面错误</h1><hr/>此页面发现一个意外错,对此我们非常抱歉。"+
"此错误消息已信息了系统管理员,请及时联系我们,我们会及时解决此问题! <br/>" +
"错误发生位置: "+Request.Url.ToString()+"<br/>"+
"错误消息: <font class=\"ErrorMessage\">"+ currentError.Message.ToString() + "</font><hr/>"+
"<b>Stack Trace:</b><br/>"+
currentError.ToString();
//如果发生致命应用程序错误
if ( !(currentError is ApplicationException) )
{
//向Windows事件日志中写入错误日志
LogEvent( currentError.ToString(), EventLogEntryType.Error );
}
//在页面中显示错误
Response.Write( errMsg );
//清除异常
Server.ClearError();
}
private void PageBase_Load(object sender, System.EventArgs e)
{ }
#endregion
}
}=============================这只是一个页面基类的例子(cs文件)!
可以看一下!
不能放到基类构造函数里,重写OnInit放到里面就可以了。
给你看下我的结构。
需要验证用户权限的页
public class Account : ZKXP.Web.User.UserBaseUserBase页继承自SiteBase页
namespace RMB.Web.User
{
public class UserBase : ZKXP.Web.SiteBase
{
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
if(BLL.Extend.GetSession("UserName") == null)
{
if((new ZKXP.BLL.UserBLL()).CookieLogin() == false)
RMB.BLL.Info.ToErrorPage("未登陆用户不能访问!");
}
}
}
}
SiteBase继承自System.Web.UI.Page
namespace ZKXP.Web
{
public class SiteBase : System.Web.UI.Page
{
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
LoadDataBaseCash();
LoadSiteSetCash();
if(WebConfig("CountOnline") == "yes")
{
(new BLL.OnLine()).CheckOnline();
}
}
}
同理AdminBase也继承自SiteBase.看下我以前帖子得出的结论就知道了。http://community.csdn.net/Expert/topic/4573/4573051.xml?temp=.4704401