asp.net 内置对象session

来源:互联网 发布:剑三喵哥可爱捏脸数据 编辑:程序博客网 时间:2024/05/19 23:54

     接触B/S结构开发以来,学习了许多的用于B/S开发的对象,如Request,Response,page,cookies、session、Application等。Request和Response还好说,request是客户端向服务器请求,Response是服务器对客户端请求的响应。至于cookies、session和application,一直弄不清他们三者之间的联系与区别。通过学习asp.net以及几天来对B/S的进一步接触,对Session又有了进一步的了解和认识。

Session

        Web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间。因此从上述的定义中我们可以看到,Session实际上是一个特定的时间概念。一个Session的概念需要包括特定的客户端,特定的服务器端以及不中断的操作时间。

A用户和C服务器建立连接时所处的Session同B用户和C服务器建立连接时所处的Session是两个不同的Session。Session 对象用于存储用户的信息。存储于 session对象中的变量持有单一用户的信息,并且对于一个应用程序中的所有页面都是可用的。由于这几个特性在网站开发过程中我们经常利用Session来储存用户信息,通过Session对话我们可以确定服务器是在跟哪个客户端交互,这样服务器就可以根据客户端的不同请求分别进行响应。

Session访问、登陆权限控制实例

     login.html接受用户登陆信息,提交给loginsuccess.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><style type="text/css">.borderStyele {border: 1px solid #3333CC;}</style></head><body><form id="form1" name="form1" method="post" action="login.aspx">  <table width="501" border="0" align="center" id="." class="borderStyele">    <tr>      <td width="164" align="right">用户名:</td>      <td width="326" align="left"><label>      <input type="text" name="userName" /><!--接受用户输入用户名-->      </label></td>    </tr>    <tr>      <td align="right">密码:</td>      <td align="left"><label>        <input type="password" name="userPwd" /><!--接受用户输入密码-->      </label></td>    </tr>    <tr>      <td colspan="2" align="center"><label>        <input type="submit" name="Submit" value="提交" /> <!--提交用户信息给loginsuccess.aspx页面-->        <input type="reset" name="Submit2" value="重置" /><!--用户信息重置-->      </label></td>    </tr>  </table></form></body></html>


      loginsuccess.aspx登陆成功后的页面,在此页面中将用户信息储存到Session中。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;public partial class login : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        string username = Request.Form.Get("userName").ToString();        Session["username"] = username;        Session["identity"]="manager"                                                                                                                             Response.Redirect("UserInfomanager.aspx");    }}


     UserInfomanager用户信息验证页面,在此页面中验证用户身份或者是否登陆。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class UserInfomanager : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (Session["username"].Equals("aaa")||Session["identify"].equals("manager"))        {            Response.Write("该用户已登录,身份验证成功!");        }           }}

           这个例子很简单,就是在客户端提交用户登陆信息后,将用户名及身份保存在Session中,在访问UserInfomanager页面时,会根据Session中的信息来判断是否响应客户端的请求。

    Session又称为会话,当客户端第一次请求时产生Session,当客户端浏览器关闭时此次Session对话结束。通过Session会话机制服务器可以区分不同客户端的请求,并对其进行响应。Session作为储存对象其权限在客户端与服务器会话请求的所有页面内有效,而且其储存特性具有单一性,在时间上随会话结束而结束。

    以上是我对Session的进一步认识,不当之处还望斧正!