代码改变世界

Web开发 前台常用方法 BasePage类

2013-07-19 13:06  -夜  阅读(205)  评论(0编辑  收藏  举报
    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");
        }
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

作者:夜 本文地址:http://www.cnblogs.com/ful1021 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。如有问题,可以邮件:531761819@qq.com 联系我,非常感谢。