【问题标题】:asp.net "Remember Me" cookieasp.net “记住我” cookie
【发布时间】:2011-03-22 07:12:53
【问题描述】:

我已经在我的 asp.net 网络表单中使用这个实现了记住我的选项,

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 身份验证,我是 not using asp.net membership..

【问题讨论】:

    标签: asp.net cookies remember-me


    【解决方案1】:

    而不是直接将用户名和密码存储在cookie中,而是将用户名和密码的哈希值和salt存储在cookie中,然后当您对cookie进行身份验证时,检索给定用户名的密码,重新创建使用密码和相同的盐进行哈希并进行比较。

    创建哈希就像将密码和盐值一起存储在一个字符串中一样简单,将字符串转换为字节数组,计算字节数组的哈希(使用 MD5 或您喜欢的任何内容)并将生成的哈希转换为一个字符串(可能通过 base64 编码)。

    下面是一些示例代码:

    // Create a hash of the given password and salt.
    public string CreateHash(string password, string salt)
    {
        // Get a byte array containing the combined password + salt.
        string authDetails = password + salt;
        byte[] authBytes = System.Text.Encoding.ASCII.GetBytes(authDetails);
    
        // Use MD5 to compute the hash of the byte array, and return the hash as
        // a Base64-encoded string.
        var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashedBytes = md5.ComputeHash(authBytes);
        string hash = Convert.ToBase64String(hashedBytes);
    
        return hash;
    }
    
    // Check to see if the given password and salt hash to the same value
    // as the given hash.
    public bool IsMatchingHash(string password, string salt, string hash)
    {
        // Recompute the hash from the given auth details, and compare it to
        // the hash provided by the cookie.
        return CreateHash(password, salt) == hash;
    }
    
    // Create an authentication cookie that stores the username and a hash of
    // the password and salt.
    public HttpCookie CreateAuthCookie(string username, string password, string salt)
    {
        // Create the cookie and set its value to the username and a hash of the
        // password and salt. Use a pipe character as a delimiter so we can
        // separate these two elements later.
        HttpCookie cookie = new HttpCookie("YourSiteCookieNameHere");
        cookie.Value = username + "|" + CreateHash(password, salt);
        return cookie;
    }
    
    // Determine whether the given authentication cookie is valid by
    // extracting the username, retrieving the saved password, recomputing its
    // hash, and comparing the hashes to see if they match. If they match,
    // then this authentication cookie is valid.
    public bool IsValidAuthCookie(HttpCookie cookie, string salt)
    {
        // Split the cookie value by the pipe delimiter.
        string[] values = cookie.Value.Split('|');
        if (values.Length != 2) return false;
    
        // Retrieve the username and hash from the split values.
        string username = values[0];
        string hash = values[1];
    
        // You'll have to provide your GetPasswordForUser function.
        string password = GetPasswordForUser(username);
    
        // Check the password and salt against the hash.
        return IsMatchingHash(password, salt, hash);
    }
    

    【讨论】:

    • @Erik 我已经将所有这些都包含在一个类中。如何在我的按钮单击时使用它们?
    • 我假设您的意思是您的登录按钮:在这种情况下,只需像往常一样获取用户名和密码,调用“CreateAuthCookie”方法传入用户名、密码和盐(这真的是任意字符串,只要您对每个方法调用使用相同的字符串) - 然后对方法返回的 cookie 做您喜欢的事情。
    • 当需要查看用户是否已经登录时,您只需按名称(“YourSiteCookieNameHere”)找到您的 cookie,然后调用“IsValidAuthCookie”方法将该 cookie 中的值与存储在数据库中的实际身份验证数据。不要忘记使用相同的盐。
    • 我不确定你要我在这里做什么......如果你已经做到了这一步,你应该能够自己做剩下的事情。毕竟——这是你的项目,不是我的。 =)
    • 太棒了! =) 很高兴听到我能提供帮助。
    【解决方案2】:

    我不会将用户密码存储在 cookie 中...而是将用户 ID 和 IP 地址存储在 cookie 中。

    【讨论】:

    • 如果用户从办公室/家庭 wifi 移动,用户必须再次登录
    【解决方案3】:

    我不会将 ip / 用户 ID 存储在 cookie 中。会话劫持将非常容易,我的意思是我知道我同事的用户名/IP,我可以将该 cookie 添加到我的消息中,然后我可以处理我同事的会话。

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 2013-01-03
      • 2012-07-10
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多