码迷,mamicode.com
首页 > 其他好文 > 详细

简单的cookie实例

时间:2015-06-03 13:11:39      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

首先新建一个简单的login.htm前端页面,利用nvelocity引擎模板。

<body>
<form action="Login.ashx" method ="post">

    <table>
        <tr><td>用户名:</td><td><input type ="text" name="UserName" value="$Model.UserName"</td></tr>
        <tr><td>密码</td><td><input type="password" name="Password" value="$Model.Password"/></td></tr>
        <tr><td></td><td><input type="submit" name="Login" value="登录" /></td></tr>
    </table>
</form>

</body>
public class CommonHelper
    {
        /// <summary>
        /// 用data填充模板渲染生成html返回
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string RenderHtml(string templateName, object data)
        {

            //模版引擎代码
            VelocityEngine vltEngine = new VelocityEngine();//实例化一个velocity模版引擎
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();
            vltContext.Put("Model",data);//设置参数,在模板中可以通过$data来引用
            //把person对象作为传入p

            Template vltTemplate = vltEngine.GetTemplate(templateName);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);

            string html = vltWriter.GetStringBuilder().ToString();//得到html这个模板
            return html;
        }
    }


再建立一个login.ashx的一般处理程序。

  public class Login : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
           string login=context.Request ["Login"];
        //取出这个对象,并且盘算是否为空
if (string .IsNullOrEmpty (login )) {           //取出cookie中的值
HttpCookie cookie2
=context.Request .Cookies["Password"]; HttpCookie cookie=context.Request .Cookies ["UserName"]; string username; if (cookie ==null ) { username = ""; } else { username = cookie.Value; } string password; if (cookie2==null ) { password = ""; } else { password = cookie2.Value; } var data = new { UserName=username,Password=password }; context.Response.Write(CommonHelper .RenderHtml ("Login.htm",data)); } else { string username=context .Request ["UserName"];           //新建cookie
HttpCookie cookie
= new HttpCookie("UserName",username);            //设定过期的时限
cookie.Expires
= DateTime.Now.AddDays(7); context.Response.SetCookie(cookie); //密码的 string password = context.Request["Password"]; //新建一个cookie实例 HttpCookie cookie2 = new HttpCookie("Password", password); //设定周期 cookie2.Expires = DateTime.Now.AddDays(5); //创建响应cookie context.Response.SetCookie(cookie2); context.Response.Write("登陆成功"); } } public bool IsReusable { get { return false; } } } }

 

简单的cookie实例

标签:

原文地址:http://www.cnblogs.com/dandandeyoushangnan/p/4548662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!