防止cookie 欺骗
时间:2011-02-17 来源:haoguowei
这个漏洞可能大家都知道,关键是怎么预防,依照我个人的理解解决方法如下:
请支持我的网站:http://www.haoy7.com 好游戏网
登陆代码:
public void Logined(ModelUser model) { int outTime = GetLoginOutTime(); HttpContext.Current.Response.Cookies["USERNAME"].Value=model.User_Name; HttpContext.Current.Response.Cookies["USERNAME_CHECK"].Value =DesSecurity.DesEncrypt(model.User_Name); HttpContext.Current.Response.Cookies["USERNAME"].Expires = DateTime.Now.AddMinutes(outTime); HttpContext.Current.Response.Cookies["USERNAME_CHECK"].Expires = DateTime.Now.AddMinutes(outTime); SetUserModel(model); }
可以看到保存了两个cookie值,都是用户名,一个是加密的一个是未加密的
验证是否登陆的代码如下:
public bool IsLogin() { bool isLogin = false; if (HttpContext.Current.Request.Cookies["USERNAME"] != null) { if (HttpContext.Current.Request.Cookies["USERNAME_CHECK"] != null) { string userName = HttpContext.Current.Request.Cookies["USERNAME"].Value; string userNameCheck = HttpContext.Current.Request.Cookies["USERNAME_CHECK"].Value; if (userName == DesSecurity.DesDecrypt(userNameCheck)) isLogin = true; } } return isLogin; }
OK了。
注意:代码是给需要的人看的,本人水平有限也不是作家请不要妄加评论,谢谢!
相关阅读 更多 +
排行榜 更多 +