如果用户没有登录就访问其他页面,那么系统会自动转向登录页,并告诉用户需要先登录。
要实现这样的功能,forms验证就是可以实现的。
这里采用的自定义基类的方式则更加灵活。
1.首先建立基类pageBase.cs
public class PageBase:System.Web.UI.Page //注意这里对System.Web.UI.Page 的继承
...{
public PageBase()
...{
}
public void PageBegin()
...{
HttpCookie cookie2 = Request.Cookies["LoginIdCookie"];
if (cookie2 != null)
...{
string LoginIdcookie = cookie2.Value;
//code here
}
else
...{
Response.Redirect("login.aspx");
}
}
}2.在除登录页面外的其他页面的load事件中进行如下:
public partial class _Default : PageBase //继承PageBase
...{
protected void Page_Load(object sender, EventArgs e)
...{
this.PageBegin(); //调用基类方法进行判断
}
}3.Login.aspx.cs
protected void BtnLogin_Click(object sender, EventArgs e)
...{
HttpCookie cookie = new HttpCookie("LoginIdCookie");
cookie.Expires.AddSeconds(20);
cookie.Value = this.TextBox1.Text;
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx");
}