第 4 节: 4-Cookie实现记住用户名
2015-07-06 14:52
239 查看
Login.html模板页:
<!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="Login.ashx" method="post"> 用户名:<input type="text" name="username" value="{username}" /> 密码:<input type="password" value="{password}" /> <input type="submit" name="btn1" value="提交" /> </form> </body> </html>
Login.ashx:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Web1.Day3; namespace Web1.Day4 { /// <summary> /// Login 的摘要说明 /// </summary> public class Login : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //1、读取模板页信息 string html = CommonHelper.ReadHtml("~/Day4/Login.html"); //2、根据是否有提交按钮这个name属性,进行判断是否是第一次打开这个页面 string btnLogin=context.Request["btn1"]; //3、判断 if (string.IsNullOrEmpty(btnLogin))//第一次打开这个页面 { //4、读取浏览器上一次关闭服务区端设置给浏览器中的cookie信息 HttpCookie cookiesLastName = context.Request.Cookies["LastUserName"]; //5\ if (cookiesLastName != null) { //6、替换模板页中的用户名占位符 html = html.Replace("{username}", cookiesLastName.Value);//-------------------------这里注意要加上value //---string 变量 = Response.Cookies["名称"].Value; } else { // 7、没有读取到用户名 html = html.Replace("{username}", ""); } //8\输出替换后的模板页 context.Response.Write(html); } else//否则用户点击 提交 按钮,就更要重新设这cookie信息 { //8.从请求报文中读取 string username = context.Request["username"]; string pwd = context.Request["password"];//---------------------------如果设置密码cookis怎么办??再实例化一个cookie??????? //9.实例化一个cookies对象,来设置cook //命名cookie HttpCookie cookLastName = new HttpCookie("LastUserName"); //赋值cookie cookLastName.Value = username; cookLastName.Expires = DateTime.Now.AddDays(7); //将cookie信息保存到报文中 context.Response.SetCookie(cookLastName); //提交成功重定向 context.Response.Redirect("MomeryTest.ashx"); } } public bool IsReusable { get { return false; } } } }
相关文章推荐
- cookie细节问题
- Cookie的写入和读取的过程
- Http协议时无状态的,没有记忆功能,连接--传送--断开
- 文件上传练习
- 练习文件保存按照日期来创建文件夹,并保存
- 第 1 节: 1-文本自增演示HttpHandler不记忆状态
- 员工Emp表的增删改查实现!本实验室目的是为了了解模板页的好处!1231312313
- 员工Emp表的增删改查实现!本实验室目的是为了了解模板页的好处!
- Companys公司表的增删该查功能实现
- C#代码试运行在服务器端----JS代码是运行在浏览器端
- 网站---不要相信浏览器
- HttpHandler动态生成图片
- ASP.Net核心对象之context.Server对象几个常用方法
- Request()方法举例
- 验证Response.End()方法的使用
- HttpResponse
- 动态调用类中的方法(无)(有)参数
- HttpRequest类
- Get与Post
- 第 3 节: 3-Http协议之Request、Response