session的介绍?

1、session是可以存取任何类型的数据的,但是cookie只能存入字符串。

2、Session读取的时候是Object类型的,所以在读取的时候要进行(强制类型的转换)

3、Session是依赖与cookie的不同浏览器之间是不能公用session

4、Session默认20分钟。 服务器压力过大可能提前就将进程内的session释放带掉

 

5、一般处理程序如果没有实现接口就会报错

6

一个简单的案例:

 

7节:

1、不建议使用table进行页面的左右和上下布局。

2、一旦抛出异常,服务器会重新启动。

1、用户名登陆界面:

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form action="sessiontest1.ashx" method="post">

        <table>

            <tr><td>用户名:</td><td><input type="text" name="username" /></td></tr>

            <tr><td>密  码:</td><td><input type="password" name="pwd" /></td></tr>

            <tr><td><input type="submit" name="btn1" value="登陆" /></td><td>{msg}</td></tr>

        </table>
    </form>

</body>
</html>

 

2、为了以后方便加处理代码,以后用户都访问.ashx,而不是直接访问html登陆页面的,好处是方便对html页面进行初始化的操作。

3、用户登陆的验证:

using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using Web1.Day3;

 

namespace Web1.Seession

{

    /// <summary>

    /// sessiontest1 的摘要说明

    /// </summary>

    public class sessiontest1 : IHttpHandler

    {

        //为了以后方便加处理代码,以后用户都访问.ashx,而不是直接访问html

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            //1、从请求报文中读取,btn1

            string btnLogin = context.Request["btn1"];

            //2、读取html页面

            string html = CommonHelper.ReadHtml("~/Seession/sessiontest1.html");

            //3、判断

            if (string.IsNullOrEmpty(btnLogin))

            {

                //4、初始化登陆页面,{msg}

                html = html.Replace("{msg}","");

                context.Response.Write(html);

            }

            else

            {

                //5、否则从请求报文中读取用户名和密码的

                string username = context.Request["username"];

                string pwd = context.Request["pwd"];

                //6、到数据库中查询

                int count = (int)SqlHelper.ExecuteScalar(

                    "select count(*) from T_Users where Name=@Name and Password=@Password",

                    new SqlParameter("@Name", username), new SqlParameter("@Password", pwd));

                //7、根据返回的整数判断

                if (count <= 0)

                {

                    //8、替换{msg}

                    html = html.Replace("{msg}", "登陆失败!");

                    context.Response.Write(html);

                }

                else

                {

                    context.Response.Redirect("ChangePassword.ashx");

                }

            }

        }

 

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

4、下面将登陆成功后的“用户名”存入到Session 中

  if (count <= 0)
                {
                    //8、替换{msg}
                    html = html.Replace("{msg}", "登陆失败!");
                    context.Response.Write(html);
                }
                else
                {
                    context.Session["loginname"]=username;//将用户名存入到session中,这样其它页面就可以读取这个session
                    context.Response.Redirect("ChangePassword.ashx");

 

5、新建一个 改变密码的一般处理程序,(只有登陆成功!才可以修改此该密码嘛!)读取Session中存入的用户名信息!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.SessionState;

 

namespace Web1.Seession

{

    /// <summary>

    /// ChangePassword 的摘要说明

    /// </summary>

    public class ChangePassword : IHttpHandler,IRequiresSessionState

    {

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

 

           string username= (string)context.Session["loginname"];

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

6、小技巧::此时做一个处理,使用一个常量,声明Session的名字,主要是改名字太长,又被多出引用!这样处理大家都错,或着都对,目的:为了避免出错!

改进如下:

 context.Session[sessiontest1.LOGINNAME]=username;//引用类名,就可以方便点出啦!

7、判断用户名是否为空

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.SessionState;

 

namespace Web1.Seession

{

    /// <summary>

    /// ChangePassword 的摘要说明

    /// </summary>

    public class ChangePassword : IHttpHandler, IRequiresSessionState//1、实现接口,这是个标志接口,里边没有方法

    {

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            //1、从session中读取用户名

            string username = (string)context.Session[sessiontest1.LOGINNAME];

            //2、判断用户名是否为空

            if (username == null)//如果没登陆,则重定向登陆页面

            {

                context.Response.Redirect("sessiontest1.ashx");
            }
            else
            {
                context.Response.Write(username + "您好,请按提示修改密码:");
            }

        }

 

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

8、对7中的判断可以处理这样的情况,当你这节访问http://localhost:55725/Seession/ChangePassword.ashx

修改密码这个页面的时候。它会先从Session中检查,用户名是否存在,不存在就会重定向登陆页面。(这个用Session这个特点~!!,其他的用请求报文Require[“username”]也行!!!)

9、同样对于其他的页面登陆检查就可以直接复用这些代码;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.SessionState; 

namespace Web1.Seession

{

    /// <summary>

    /// QueryYuE 的摘要说明

    /// </summary>

    public class QueryYuE : IHttpHandler, IRequiresSessionState//1、实现接口,这是个标志接口,里边没有方法
    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            

            //这里做个用户名是否存在检查,,直接复用代码就行-------------

            context.Response.ContentType = "text/html";

            //1、从session中读取用户名

            string username = (string)context.Session[sessiontest1.LOGINNAME];

            //2、判断用户名是否为空

            if (username == null)//如果没登陆,则重定向登陆页面

            {

                context.Response.Redirect("sessiontest1.ashx");//返回登陆页

            }

            else

            {

                context.Response.Write(username + "您好,欢迎查看余额:");

            }

        }

 

        public bool IsReusable
        {
            get
            {
               return false;
            }
        }
    }
}

 

10、问题:当你在上网的时候,例如百度云,你没有输入账号,就打开了一个页面,当你输入你的账号后,为了,实现,页面登陆后重新,跳转会原来的登陆页面;(简记:从哪个页面重定向登陆的,登陆后还重定向这个页面

解决:1)在登陆一般处理程序中设定一个常量

public const string LOGINBEFOREURL = "loginTryUrl";//尝试登陆时候的页面地址

 

2)来到修改密码一般处理程序中,此时如果用户名为空,就会跳转到登陆页面,此时,当前修改密码页面的url地址存到一个Session中。

  //2、判断用户名是否为空

            if (username == null)//如果没登陆,则重定向登陆页面

            {

                //11、获取当前地址,并存入到session中(存),地址记得类型转换

                context.Session[sessiontest1.LOGINBEFOREURL] = context.Request.Url.ToString();

                context.Response.Redirect("sessiontest1.ashx");//返回登陆页面

 

            }

            else

            {

                context.Response.Write(username + "您好,请按提示修改密码:");

         }

 

 

3)返回登陆页面进行,读取Session

  //7、根据返回的整数判断

                if (count <= 0)

                {

                    //8、替换{msg}

                    html = html.Replace("{msg}", "登陆失败!");

                    context.Response.Write(html);

                }

                else

                {

                    //9、登陆成功,页面跳转!并//将用户名存入到session中,这样其它页面就可以读取这个session

                    context.Session[sessiontest1.LOGINNAME]=username;

                    // 这  context.Response.Redirect("ChangePassword.ashx");

 

                    //12、读取存入登陆前页面的url地址,从Session中(读)

                    string navUrl =(string)context.Session[sessiontest1.LOGINBEFOREURL];

                    //13、如果你登陆前的地址有,就重定向登陆前的页面

                    if (navUrl!=null)

                    {

                        context.Response.Redirect(navUrl);

                    }

       }

 

11、可以通过开发者工具查看;

 

 

 

12、(不好理解,再看~~~~~~~~~~~~~~~~~

13、增加退出登陆的超链接!(使用这个方法)

 

15、创建推出登陆一般处理程序

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.SessionState;

namespace Web1.Seession

{

    /// <summary>

    /// LoginOut 的摘要说明

    /// </summary>

    public class LoginOut : IHttpHandler, IRequiresSessionState

    {

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            //1、从服务器中销毁session

            context.Session.Abandon();

            //2、session销毁,表示用户名丢失。。所以重定向登陆页面

            context.Response.Redirect("sessiontest1.ashx");

        }

 

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

16、设置一个推出连接QueryYuE .ashx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.SessionState;

 

namespace Web1.Seession

{

    /// <summary>

    /// QueryYuE 的摘要说明

    /// </summary>

    public class QueryYuE : IHttpHandler, IRequiresSessionState//1、实现接口,这是个标志接口,里边没有方法

    {

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            

            //这里做个用户名是否存在检查,,直接复用代码就行-------------

            context.Response.ContentType = "text/html";

            //1、从session中读取用户名

            string username = (string)context.Session[sessiontest1.LOGINNAME];

            //2、判断用户名是否为空

            if (username == null)//如果没登陆,则重定向登陆页面

            {

                context.Response.Redirect("sessiontest1.ashx");//返回登陆页面

 

            }

            else

            {

                context.Response.Write(username + "您好,欢迎查看余额:"+"<a href='LoginOut.ashx'>推出登陆</a>");

            }

        }

 

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

17、在导航页面进行重定向设置

using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.SessionState;

using Web1.Day3;

 

namespace Web1.Seession

{

    /// <summary>

    /// sessiontest1 的摘要说明

    /// </summary>

    public class sessiontest1 : IHttpHandler, IRequiresSessionState//10、 实现接口,shift+alt+f10导入命名

    {

        public const string LOGINNAME = "loginname";

        public const string LOGINBEFOREURL = "loginTryUrl";//尝试登陆时候的页面地址

 

        //为了以后方便加处理代码,以后用户都访问.ashx,而不是直接访问html

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            //1、从请求报文中读取,btn1

            string btnLogin = context.Request["btn1"];

            //2、读取html页面

            string html = CommonHelper.ReadHtml("~/Seession/sessiontest1.html");

            //3、判断

            if (string.IsNullOrEmpty(btnLogin))

            {

                //4、初始化登陆页面,{msg}

                html = html.Replace("{msg}", "");

                context.Response.Write(html);

            }

            else

            {

                //5、否则从请求报文中读取用户名和密码的

                string username = context.Request["username"];

                string pwd = context.Request["pwd"];

                //6、到数据库中查询

                int count = (int)SqlHelper.ExecuteScalar(

                    "select count(*) from T_Users where Name=@Name and Password=@Password",

                    new SqlParameter("@Name", username), new SqlParameter("@Password", pwd));

                //7、根据返回的整数判断

                if (count <= 0)

                {

                    //8、替换{msg}

                    html = html.Replace("{msg}", "登陆失败!");

                    context.Response.Write(html);

                }

                else

                {

                    //9、登陆成功,页面跳转!并//将用户名存入到session中,这样其它页面就可以读取这个session

                    context.Session[sessiontest1.LOGINNAME] = username;

 

 

                    //12、读取存入登陆前页面的url地址,从Session中(读)

                    string navUrl = (string)context.Session[sessiontest1.LOGINBEFOREURL];

                    //13、如果你登陆前的地址有,就重定向登陆前的页面

                    if (navUrl != null)

                    {

                        context.Response.Redirect(navUrl);

                    }

                    else

                    {

                        context.Response.Redirect("ChangePassword.ashx");//默认进入密码修改页

                    }

 

                }

            }

        }

 

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

 

转载于:https://www.cnblogs.com/xcl461330197/articles/4560248.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值