当前位置:网站首页>防止Cookie修改id欺骗登录
防止Cookie修改id欺骗登录
2022-07-27 06:59:00 【丁亚涛】
在开发网站时,经常需要通过保存在Cookie中的id恢复Session登录。恶意用户通过修改Cookie中的id欺骗登录,例如,在开发者模式下:

笔者设计了自动重新申请session的机制(具体见前期文章),当session过期,可以创建新的session续航。为了防止修改id,可以采用校验码机制:
(1)登录时创建校验码:check_key并生成验证序列check_code
(2)将id和验证串check_code保存到cookie中,将check_key保存到用户表记录字段中
(3)需要通过cookie恢复网页时,将cookie中的验证串和通过用户表存储的check_key计算出来的验证串作比对,从而实现防欺骗
(4)由于cookie中的id附加了验证串,在登录时需要清除,防止文本框(设id为txtUserID)自动搜索填充:
$(document).ready(function () {
var c_input = $("#txtUserID").val();
if (c_input.indexOf("|") != -1)
$("#txtUserID").val(c_input.split("|")[0]);
});(5)网页中的验证:
HttpCookie readcookie = Request.Cookies["userID"];
string[] a_uid = readcookie.Value.ToString().Split('|');
string cuid = a_uid[0];
string ccheck = a_uid[1];
if (Users.CookieCheck(cuid, ccheck) == false) Response.Redirect("login.aspx");(6)Users类创建验证方法
public bool CookieCheck(string cuserid, string ccheckstring)
{
DataBase DB = new DataBase();
string sql = "select user_check from users where USERID='" + cuserid + "'";
DataSet ds = DB.GetDataSetSql(sql);
if (ds.Tables[0].Rows.Count <= 0)
{
return false;
}
else
{
string c_user_check = ds.Tables[0].Rows[0].ItemArray[0].ToString();
if (String.IsNullOrEmpty(c_user_check) || c_user_check.ToLower()=="null") return false;
int nkey = int.Parse(ds.Tables[0].Rows[0].ItemArray[1].ToString());
if (ccheckstring == CreateCookieCheck(cuserid, nkey))
return true;
else
return false;
}
}
public string CreateCookieCheck(string cuserid, int nkey)
{
//根据cuserid和nkey生成验证串
......
}边栏推荐
- flink去重(二)解决flink、flink-sql去重过程中的热点问题
- 网络入门——vlan及trunk概述
- Gossip: talk with your daughter about why you should learn culture lessons well
- 临界区(critical section 每个线程中访问 临界资源 的那段代码)和互斥锁(mutex)的区别(进程间互斥量、共享内存、虚拟地址)
- functools模块
- js正则表达式实现每三位数字加一个逗号
- Introduction to network -- overview of VLAN and trunk
- 用户解锁SM04 SM12
- Panabit SNMP配置
- Using docker in MAC to build Oracle database server
猜你喜欢

什么是真正的 HTAP ?(二)挑战篇

C common function integration-3

Properties类和properties配置文件的理解学习

vlan间路由(讲解+验证)

(2022牛客多校三)A-Ancestor(LCA)

SQLite common function integration

Single arm routing (explanation + experiment)

Expose Prometheus metrics in Perl programs

Bingbing's learning notes: classes and objects (middle)

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
随机推荐
Apifox安装及使用
Perl: split the external command to be executed into multiple lines
Chapter 6 Shell Logic and Arithmetic
闭散列和开散列解决哈希冲突
面试复盘五
Turn off the auto start function of Oracle service in centos7
海康h9摄像头用xshell无法连接(没有启用ssh)
Cadence(十一)丝印调整和后续事项
Mysql: increase the maximum number of connections
连接MySQL时报错:Public Key Retrieval is not allowed 【解决方法】
Codeforces Round #810 (Div.2) A-C
C语言实现猜数字小游戏项目实战(基于srand函数、rand函数,Switch语句、while循环、if条件判据等)
一体化实时HTAP数据库StoneDB,如何替换MySQL并实现近百倍分析性能的提升
Temperature and humidity measurement and display device based on Arduino
Properties类和properties配置文件的理解学习
flink去重(二)解决flink、flink-sql去重过程中的热点问题
IDEA中文乱码怎么办
我是不是被代码给耽误了……不幸沦为一名程序员……
ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
小程序支付管理-新版支付对接流程