WEB系统的安全登录与退出

本文主要讲解的是在登录系统过程中防止从其他界面的地址登录,在安全退出系统之后。又如何防止,通过浏览器的后退按钮又进入到系统,本文主要针对的是刚入门的一些菜鸟,大牛请绕过。        

  登录代码如下: 

      FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(
                    1, Server.UrlEncode(strUserName), DateTime.Now, DateTime.Now.AddMinutes(180), true, Server.UrlEncode(strUserName));
                string encryptedTicket = FormsAuthentication.Encrypt(Ticket);// 对票证加密
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);//创建新的cookie并为其赋值
                authCookie.HttpOnly = true; //可以通过客户端脚本访问
                authCookie.Path = FormsAuthentication.FormsCookiePath;//
                authCookie.Secure = FormsAuthentication.RequireSSL;
                Response.Cookies.Add(authCookie);

本文主要是应用了票据验证方法,在登陆的过程中将登陆用户的信息保存到cookie,可以保存用户的用户名、也可以保存用户的物理地址,cookie的作用非常强大,本文不重点介绍。

      其次在基类中判断用户是否合法登陆。

      protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Page.Response.Write("<script>window.top.location.href='Load.aspx';</script>");
            }
            else
            {
                if (string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
                {
                    Page.Response.Write("<script>window.top.location.href='Load.aspx';</script>");
                }
            }
        }

 

 

在安全退出的过程中,将票据清除掉,然后跳转到任何一个继承了BasePage的类的界面中即可。 具体的Demo我会放上来,大家有空可以多了解下票据验证和Cookie,对于我们这些新人,这些安全性的东西还是比较有诱惑的。本人联系方式:521070107  王永鹏  可以一起讨论讨论技术哦。

posted on 2012-09-04 14:51  我爱毛毛  阅读(458)  评论(1编辑  收藏  举报