asp.net“记住我”曲奇饼
我已经使用它在我的 asp.net webform 中实现了“记住我”选项,
protected void LBtnSubmit_Click(object sender, EventArgs e)
{
if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
{
HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text);
cookie.Expires.AddYears(1);
Response.Cookies.Add(cookie);
}
}
我这样做的方式正确吗?任何建议..我正在使用 Windows 身份验证,并且我不使用 asp.net 会员资格
..
I have implemented remember me option in my asp.net webform by using this,
protected void LBtnSubmit_Click(object sender, EventArgs e)
{
if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
{
HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text);
cookie.Expires.AddYears(1);
Response.Cookies.Add(cookie);
}
}
Am i doing it the right way? Any suggestion.. I am using windows authentication and i am not using asp.net membership
..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要直接将用户名和密码存储在 cookie 中,而是将用户名和密码的哈希值以及盐存储在 cookie 中,然后当您验证 cookie 时,检索给定用户名的密码,使用密码和相同的盐并进行比较。
创建哈希非常简单,只需将密码和盐值一起存储在字符串中,将字符串转换为字节数组,计算字节数组的哈希(使用 MD5 或您喜欢的任何内容)并将生成的哈希转换为字符串(可能通过base64编码)。
这是一些示例代码:
Rather than directly storing the username and password in the cookie, store the username and a hash of the password and a salt in the cookie, then when you authenticate the cookie, retrieve the password for the given username, re-create the hash with the password and the same salt and compare them.
Creating the hash is as simple as storing the password and salt values together in a string, converting the string to a byte array, computing the hash of the byte array (using MD5 or whatever you prefer) and converting the resulting hash to a string (probably via base64 encoding).
Here's some example code:
我不会将用户密码存储在 cookie 中...而是将用户 ID 和 IP 地址存储在 cookie 中。
I would not store the users password in a cookie... Rather store the user id and the ip address in the cookie.
我不会将 ip / 用户 ID 存储在 cookie 中。会话劫持将非常容易,我的意思是我知道我同事的用户名/IP,我可以将该 cookie 添加到我的消息中,然后我可以处理我同事的会话。
I would not store the ip / user id in the cookie. Session highjacking would then be really easy, I mean I know the username / ip of my collegues, I could add that cookie to my message and then I can work on the session of my collegue.