当前位置:网站首页>防止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生成验证串
......
}边栏推荐
- Routing between VLANs (explanation + verification)
- Jjwt generate token
- JS make a traffic light
- STM32_找到导致进入HardFault_Handler的函数
- Multithreading [preliminary - Part 1]
- JS regular expression implementation adds a comma to every three digits
- A priority SQL problem
- flink1.14 sql基础语法(一) flink sql表查询详解
- ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
- (2022牛客多校三)J-Journey(dijkstra)
猜你喜欢

STM32_ Find the cause of entering hardfault_ Handler's function

我是不是被代码给耽误了……不幸沦为一名程序员……

Cadence(十一)丝印调整和后续事项

Chapter 6 Shell Logic and Arithmetic

ADC噪声全面分析 -01- ADC噪声的类型以及ADC特性

Actual combat of flutter - Request encapsulation (I)

小程序支付管理-新版支付对接流程

Chapter 6 Shell Logic and Arithmetic

Zabbix: map collected values to readable statements

单臂路由(讲解+实验)
随机推荐
(2022 Hangdian multi school III) 1011.taxi (Manhattan maximum + 2 points)
Temperature and humidity measurement and display device based on Arduino
Properties类和properties配置文件的理解学习
Port forwarding summary
C language pthread_ cleanup_ Push() and pthread_ cleanup_ Pop() function (used for the resource cleaning task after the termination action in the critical resource program segment to avoid deadlock. T
flink中维表Join几种常见方式总结
Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query
2022-07-25 Gu Yujia's study notes
电子量产项目框架--基本思想
Demonstrate the use of foreign keys with Oracle
Introduction to network -- overview of VLAN and trunk
Mysql: increase the maximum number of connections
小程序支付管理-新版支付对接流程
Pg_ relation_ Size question
Use shell to calculate the sum of numbers in text
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
flink原理(一) 状态的TTL管理、容错机制
杂谈:跟女儿聊为啥要学好文化课
Error when connecting to MySQL: public key retrieval is not allowed [solution]
The DrawImage method calls the solution of not displaying pictures for the first time