public class BasePage : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Session["id"] == null || Session["loginId"] == null)
{
Response.Redirect("Login.aspx");
Response.End();
}
}
public int UserId
{
get { return Convert.ToInt32(Session["id"]); }
}
public string LoginId
{
get { return HttpUtility.UrlDecode(Session["loginId"].ToString()); }
}
public string Name
{
get { return HttpUtility.UrlDecode(Session["name"].ToString()); }
}
/// <summary>
/// 退出登录
/// </summary>
public void Logout()
{
HttpContext.Current.Session["id"] = null;
HttpContext.Current.Session["loginId"] = null;
HttpContext.Current.Session["name"] = null;
HttpContext.Current.Session.Abandon();
if (HttpContext.Current.Request.Cookies["id"] != null) { CookieHelper.DelCookie("id"); }
if (HttpContext.Current.Request.Cookies["loginId"] != null) { CookieHelper.DelCookie("loginId"); }
if (HttpContext.Current.Request.Cookies["pwd"] != null) { CookieHelper.DelCookie("pwd"); }
HttpContext.Current.Response.Redirect("/Login.aspx");
}
}Web开发 前台常用方法 BasePage类
最新推荐文章于 2024-07-01 15:32:10 发布
本文介绍了一个基类BasePage,用于ASP.NET应用程序中的页面继承。该类通过检查Session变量来实现登录验证,并提供了用户ID、登录ID及名称的属性访问器。此外,还包括一个Logout方法用于清除Session并重定向到登录页面。
753

被折叠的 条评论
为什么被折叠?



