继承PAGE类后,把认证写在构造函数里不知为什么出错?
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// myAdminPage 的摘要说明
/// </summary>
public class myAdminPage : myPage
{
public myAdminPage()
{
//
// TODO: 在此处添加构造函数逻辑
//
isLogin();
}
public void isLogin()
{
if ( System.Web.HttpContext.Current.Session[ "AdminName "] == null )
{
if (System.Web.HttpContext.Current.Session[ "AdminName "].ToString() != "Zq3L ")
{
pc.Redirect( "Login.aspx ");
}
}
}
}
请问错在哪里呢?
------解决方案--------------------if ( System.Web.HttpContext.Current.Session[ "AdminName "] == null )
===================================================================
if ( System.Web.HttpContext.Current.Session[ "AdminName "] != null )
------解决方案--------------------是啊,在!=NULL里面再做处理
这里还缺一个ELSE操作
if ( System.Web.HttpContext.Current.Session[ "AdminName "] != null )
{
if (System.Web.HttpContext.Current.Session[ "AdminName "].ToString() != "Zq3L ")
{
pc.Redirect( "Login.aspx ");
}
}
else
pc.Redirect( "Login.aspx ");
------解决方案--------------------写个基类,然后要要验证的页面继承该基类。
using System;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Diagnostics;
namespace KingdeeWeb
{
/// <summary>
/// PageBase 的摘要说明。
/// </summary>
public class PageBase : System.Web.UI.Page
{
#region Web 窗体设计器生成的代码
protected override void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
base.OnInit(e);
this.Load+=new System.EventHandler(this.PageBase_Load);
}
private void PageBase_Load(object sender,System.EventArgs e)
{
this.PageBegin();
}
#endregion
public void PageBegin()
{
if( (UserInfo)Session[ "UserInfo "] ==null )
{
string Url = Request.ApplicationPath.ToString()+ "/Default.aspx " ;
Response.Write( " <script> alert( '在线超时或非法登陆,