Cookie登录保存

Login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="CZBK.ItcastProject.WebApp._2015_5_31.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>
            用户名:<input type="text" name="txtName" value="<%=LoginUserName %>" /><br />
            密码:<input type="password" name="txtPwd" value="<%=LoginPwd %>" /><br />
            <input type="submit" value="登录" />
        </div>
    </form>
</body>
</html>

Login.aspx.cs

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

namespace CZBK.ItcastProject.WebApp._2015_5_31
{
    public partial class Login : System.Web.UI.Page
    {
        public string LoginUserName { get; set; }
        public string LoginPwd { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string userName = Request.Form["txtName"];
                string userPwd = Request.Form["txtPwd"];
                //写到Cookie中
                Response.Cookies["userName"].Value = Server.UrlEncode(userName);
                Response.Cookies["userPwd"].Value = Server.UrlEncode(userPwd);
                Response.Cookies["userName"].Expires = DateTime.Now.AddDays(7);
                Response.Cookies["userPwd"].Expires = DateTime.Now.AddDays(7);
                //读取cookie.
                if (Request.Cookies["userName"] != null)
                {
                    string name = Server.UrlDecode(Request.Cookies["userName"].Value);
                    LoginUserName = name;
                }
                if (Request.Cookies["userPwd"] != null)
                {
                    string pwd = Server.UrlDecode(Request.Cookies["userPwd"].Value);
                    LoginPwd = pwd;
                }
            }
            else
            {
                //读取cookie.
                if (Request.Cookies["userName"]!=null)
                {
                    string name = Server.UrlDecode(Request.Cookies["userName"].Value);
                    LoginUserName = name;
                }
                if (Request.Cookies["userPwd"] != null)
                {
                    string pwd = Server.UrlDecode(Request.Cookies["userPwd"].Value);
                    LoginPwd = pwd;
                }
            }
        }
    }
}
时间: 2024-08-06 09:19:14

Cookie登录保存的相关文章

Android WebView保存Cookie登录

因项目需要,需要在App中嵌入网页,使用Nativie方式登录,然后将cookie保存到WebView中,实现免登录功能.同步Cookie到WebView的方法网上有大量的参考资料,也可以参考下面的代码: /** * Sync Cookie */ private void syncCookie(Context context, String url){         try{             Log.d("Nat: webView.syncCookie.url", url);

JMeter学习-018-JMeter 配置元件之-HTTP信息头管理器-实现 Cookie 登录

之前写过一篇通过[HTTP Cookie管理器]实现登录态操作的文章,使用时需要配置每个键值对(如下图所示),相对来讲配置比较繁琐.其实,我们也可通过在[HTTP信息头管理器]添加 Cookie 信息,从而实现登录态操作. 操作步骤如下: 1.获取 Cookie(通过浏览器等,如下 Firefox 浏览器所示),例如通过浏览器获取 Cookie 信息,保存到文本 cookie.txt 中 2.通过 [CSV Data Set Config]读取 cookie.txt 中的 cookie 信息(可

python爬虫-使用cookie登录

前言: 什么是cookie? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密). 比如说有些网站需要登录后才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么我们可以利用Urllib库保存我们登录的Cookie,然后再抓取其他页面,这样就达到了我们的目的. 一.Urllib库简介 Urllib是python内置的HTTP请求库,官方地址:https://docs.python.org/3/library/urllib.ht

人人网(cookie登录)

body { background-color: #f4ede3 } .box { width: 600px } .cnblogs_code { background-color: #f5f5f5; font-family: Courier New !important; font-size: 12px !important; border: 1px solid #ccc; padding: 5px; overflow: auto; margin: 5px 0; color: #000 } 有时

JavaWeb--Servlet过滤器Filter和SpringMVC的HandlerInterceptor(Session和Cookie登录认证)

拦截一些请求进行处理,比如通过它来进行权限验证,或者是来判断用户是否登陆,日志记录,编码,或者限制时间点访问等等,是非常有必要的.所以就有了此篇文章啦. 文章结构:(1)Servlet过滤器Filter:(2)SpringMVC的HandlerInterceptor:(3)对比认知. 一.Servlet过滤器Filter: 此部分是从赵四大佬那里学来的,并补充自己的认知 (1)概念: 能够对Servlet容器的请求和响应对象进行检查和修改. Servlet过滤器本身并不产生请求和响应对象,它只能

C# Cookies跨域登陆(写入cookie登录)

//写入cookie登录记录作为其它系统使用 public void writeCookie(string user, string password, string Ulevel, string Utype) { //deleteCookie();//增记录时删除以前的用户记录 HttpContext.Current.Response.Cookies["UserName"].Value = user; HttpContext.Current.Response.Cookies[&quo

[zz]【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie

http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file/ #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: [整理]Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie http://www.crifan.com/python_auto_handle_cookie_an

scrapy带cookie登录简单的网站

在爬数据的时候,登录一直是一个比较麻烦的问题.我也一直在网上找过各种资料,都挺麻烦的,因为需要分析各种http过程,感觉太麻烦了. 不过最近在一个同学的帮助下,找到了使用cookie登录的方法.因为带cookie登录的话,server会认为你是一个已登录的用户,所以就会返回给你一个已登录的内容 本文介绍的方法,是使用python的scrapy框架登录的方法.而且也只能登录一些比较简单的网站,对于那种大型的网站,因为目前我也还没有验证过,所以不敢保证.但是经过验证,登录豆瓣是万试万灵的. 1. 获

JAVA- JSP中解决无法在Cookie当中保存中文字符的问题

因为cookie的值是ASCII字符,不能直接把自定义cookie的值直接赋值为中文,但是要实现这个功能,还是有方法的. 1.java中已经给我们提供了方法,此时只需要导入该包就行 <%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%> 2.解决页面中文字符出现乱码问题 <body>