说明:cookie常用于数据保存

1 使用

  //创建cookie

  Response.cookies["yk"].value ="xyxtl";

  //设置过期时间     如果不设置cookie只存在内存中,无法实现长期保存

  Response.cookies["yk"].Expires = DateTime.Now.AddDays(3);

  //删除cookie  == 修改过期时间

  Response.cookies["yk"].Expires = DateTime.Now.AddDays(-3);

  //cookie跨域 正常的情况主域可以自动向子域传递数据,子域无法向主域传递数据

  Response.cookies["yk"].Domain = "主域网址";

  //cookie使用范围

   Response.cookies["yk"].Path = ...

二:C#中的使用实例:记住密码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01Cookie记住密码.aspx.cs" Inherits="_03状态保持_Cookies和Session_._01Cookie记住密码" %>

<!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" method="post" >
用户名:<input type="text" name="UserName"/>
<br />
密码:<input type="password" name="Pwd"/>
<br />
<input type="checkbox" id="Remember" name="Remember" />记住我1周
<input type="checkbox" id="Forget" name="Forget" />退出
<br />
<input type="submit" value="登录" />
</form>
</body>
</html>

aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace _03状态保持_Cookies和Session_
{
public partial class _01Cookie记住密码 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//写入cookie
if ( Request["Remember"]=="on")
{
string userName = Request["UserName"];
string pwd = Request["Pwd"];
Response.Cookies[userName].Value = pwd;
Response.Cookies[userName].Expires = DateTime.Now.AddDays(); }
//删除cookie
if ( Request["Forget"] == "on")
{
string userName = Request["UserName"];
string pwd = Request["Pwd"];
Response.Cookies[userName].Value = pwd;
Response.Cookies[userName].Expires = DateTime.Now.AddDays(-); }
//判断cookie
if ( (Request.Cookies["yk"]) != null)
{
if (Request.Cookies["yk"].Value == "")
{
Response.Write("登录成功!");
Response.Write(Request["category"]);
}
else {
Response.Write("登录失败!");
Response.Write(Request["category"]);
}
}
else
{
if (!String.IsNullOrEmpty(Request["UserName"]) && !String.IsNullOrEmpty(Request["Pwd"]) && Request["UserName"] == "yk" && Request["Pwd"]=="")
{
Response.Write("登录成功!");
Response.Write(Request["category"]);
}
else {
Response.Write("登录失败!");
Response.Write(Request["category"]);
}
}
}
}
}

aspx.cs

在不使用cookie记录前登录是失败的


三:python 中使用cookie直接访问index页面

1:注释掉setting.py的中间件配置

2:配置路由

"""cookie_Session URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app011 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login),
path('index/', views.index), ]

urls.py

from django.shortcuts import render,redirect

# Create your views here.
def login(request):
# print("SSSSSSSSS",request.session)
if request.method=="POST":
name=request.POST.get("user")
pwd = request.POST.get("pwd")
if name=="aaron" and pwd=="":
ret = redirect("/index/")
ret.set_cookie("user",name)
ret.set_cookie("password",pwd)
return ret
return render(request,"login.html") def index(request): if request.COOKIES.get("user",None) == "aaron":
name = "aaron"
return render(request, "index.html",locals())
else:
return redirect("/login/")

Views.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ name }}</h1>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
*{
margin: 0;
padding:0;
}
</style>
</head>
<body>
<form action="/login/" method="post">
<p>姓名<input type="text" name="user"></p>
<p>密码<input type="password" name="pwd"></p>
<p><input type="submit"></p> </form>
</body>
</html>

login.html

最新文章

  1. php 执行计划任务方式之 linux crontab 执行命令
  2. [ERROR] Fatal error: Can&#39;t open and lock privilege tables: Table &#39;mysql.host&#39; doesn&#39;t exist
  3. MQTT for UWP
  4. 【2013微软面试题】输出节点数为n的二叉树的所有形态
  5. CentOS系统内核升级
  6. 图像色彩空间YUV和RGB的差别
  7. 【Oracle】ORA-01722:无效数字(控制文件最后一个字段)
  8. 【C#】Smtp发送邮件
  9. jQuery的工作原理
  10. ASP.NET MVC, Url长度过长问题解决,404.15问题
  11. Python爬虫入门教程 44-100 Charles的安装与使用-手机APP爬虫部分
  12. 利用cocoapods管理开源项目,支持 pod install安装整个流程记录(github公有库)
  13. java子类继承关系
  14. Spark ML机器学习
  15. C++并发编程之std::future
  16. 【MVC】使用FormCollection获取Form表单数据
  17. cesium编程入门(六)添加 3D Tiles,并调整位置,贴地
  18. myeclipse工程中library 和 web-inf下lib的区别
  19. Hadoop压缩之CompressionCodecFactory
  20. 使用免安装版本在windows上手动安装PostgreSQL

热门文章

  1. 关于树的常见操作-C++面试
  2. 机器学习与AI相关的资料
  3. layout 的应用
  4. Qt图片显示
  5. LA 6893 矩阵HASH (模板)
  6. JetBrains GoLand 2018 激活码/ 注册码(最新破解方法)
  7. Python-select 关键字 多表查询 子查询
  8. Jenkins五 配置tomcat
  9. Oracle11g 体系结构
  10. Confluence 6 从你的 JDBC 连接中直接启用校验查询