您的位置:首页 > 移动开发

【.net基础】--Application,Session,Cookie你能分的清吗?

2015-05-06 16:12 489 查看

背景:

  每个东东产生的时候都是有原因滴,我个人觉得,这几种方式跟缓存很类似,都是把常用的一些数据放到缓存里面先存起来,比直接从内存或硬盘中读取更加的迅速,从而提高效率。Application,Session,Cookie是Asp.Net中常用的三种存取临时数据的方法。  (常用内置对象)

Application/session/Cookie对比:

对比
Session
Application
Cookie
作用
用于保护用户的专用信息
用于保存所有用户的公共数据信息。
用于保护客户浏览器请求服务器页面的请求信息
使用
多人
全局
单个
共享
No
Yes
No
有效期
个人退出20分钟(可设),存数据效率低
程序周期,
会消失,考虑写入文件或数据库
访问过大会造成性能瓶颈
自定义,关闭浏览器会消失
数据
少,简单,单个用户,单个链接
任意
少,简单,非敏感
位置
服务器端
服务器端
客户端

登陆实战演练:

        用户登陆采用Cookie保存用户信息,Session保存用户名,Application计算访问量。整个业务逻辑是,用户先进行登陆,登陆了之后进入一个新的页面,显示登陆的人数和登陆者的姓名。

login.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace demo5
{
public partial class Login : System.Web.UI.Page
{

//登陆时点击登陆按钮

protected void btnSubmit_Click(object sender, EventArgs e)
{
//写入cookie
Response.Cookies["username1"].Value = txtUserName.Text;
//设置cookie保存时间
Response.Cookies["username1"].Expires = DateTime.Now.AddDays(1);
//判断用户名是否为空
if (Request.Cookies["username1"] != null)
{   //session获取用户名
Session["username1"] = Request.Cookies["username1"].Value;
//跳转到欢迎页面
Response.Redirect("Welcome.aspx");
}

}

}
}


login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="demo5.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="登陆" />

</div>

</form>

</body>
</html>


结果页面:



跳转的新的页面:

 Welcome.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace demo5
{
public partial class Welcome : System.Web.UI.Page
{

//加载欢迎页面
protected void Page_Load(object sender, EventArgs e)
{
//如果session内部的用户名为空
if (Session["username1"] != null)
{//打印字符串内容
string conent = "欢迎" + Session["username1"] + "进入ASP.NET";
Response.Write(conent);
//如果全局变量不为空
if (Application["SessionCount"] != null)
{
labApplication.Text = Session["username1"] + "共登陆了" + Application["SessionCount"].ToString() + "次";
}
}

}

}
}


Welcome.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="demo5.Welcome" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="labApplication" runat="server" ></asp:Label>

</div>
</form>
</body>
</html>


global.asax

protected void Application_Start(object sender, EventArgs e)
{

//全局计数器 计数+1
Application["SessionCount"] = 0;

}

protected void Session_Start(object sender, EventArgs e)
{
Session["username1"] = 0;
//在新回话启动时运行的代码
Application.Lock();
//全局计数器 计数+1
Application["SessionCount"] = (int)Application["SessionCount"] + 1;
Application.UnLock();

}


登陆成功页:



现象分析:

1.如果登陆了之后,再打开一个浏览器页面,浏览同样的网址,登陆次数会+1,说明Application在整个程序的生命周期中都起作用,而且可以被多个用户访问,保存公共的用户数据信息.

2.有的时候,关闭了浏览器,立即重新启动程序,用户名仍然是之前的那个用户名,证明Session是具有时效性的。

          

总结:

       做demo的时候并不是很顺利,不过完成之后是很有成就感的,80分有的时候真的比100分实惠。

2015年7月16日补充:

Cookie的存入:

//给定的用户名创建身份验证 Cookie
HttpCookie cookie = FormsAuthentication.GetAuthCookie(model.UserName, model.RememberMe);
cookie.Name = "selfUserInfo";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);


Cookie的取出:

if (Request.Cookies["selfUserInfo"] != null)
{
HttpCookie mycookie;
mycookie = Request.Cookies["selfUserInfo"];
Response.Cookies["selfUserInfo"].Expires = System.DateTime.Now.AddMonths(-1);//cookie过期处理
Response.Cookies.Remove("selfUserInfo");//清除
Response.Cookies.Add(mycookie);//写入立即过期的
Response.Cookies["selfUserInfo"].Expires = DateTime.Now.AddDays(-1);
}
//从cookie中返回用户信息

var memberValidation = HttpContext.Request.Cookies.Get("selfUserInfo")


应用场景:

        .NET单点登录的时候,根据用户名可以把用户登录验证产生的票据信息存入Cookie中,也可以根据用户名取出Cookie.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: