我正在使用BCrypt的.Net实现来在数据库中存储密码。密码列为VARCHAR(MAX)
这是通过存储过程更新密码的代码:
Update [User]
Set [Password]= @NewPassword,
ModifiedOn = GetDate(),
ModifiedBy = 'PasswordChanger'
Where [UserName] = @UserName
对于某些用户,密码会被截断。截断后的一个例子是:$ 2a $ 12 $ XM2
事实并非如此。
请帮我理解可能导致截断的原因?
更新:
以下是调用SP来更新密码的C#代码:
string HashedPassword;
int NumberOfRowsAffected;
try
{
Database jss = DatabaseFactory.CreateDatabase();
HashedPassword = BCrypt.HashPassword(txtPassword.Text, BCrypt.GenerateSalt(12));
NumberOfRowsAffected = jss.ExecuteNonQuery("procUpdatePassword", GetLogin(HttpContext.Current.User.Identity), HashedPassword);
if (NumberOfRowsAffected > 0)
lblStatus.Text = "Password updated.";
else
{
lblStatus.Text = "Password not updated for this user.";
}
}
catch (Exception ex)
{
lblStatus.Text = "Password was not changed due to an error.";
lblStatus.Text += ex.ToString();
}
答案 0 :(得分:1)
最后,双方都应该与表模式同步。
示例代码......
using (SqlConnection con = new SqlConnection("Your Connection String"))
{
using (SqlCommand cmd = new SqlCommand("Your Stored Procedure Name", con))
{
SqlParameter param = new SqlParameter();
param.ParameterName = "Parameter Name";
param.Value = "Value";
param.SqlDbType = SqlDbType.VarChar;
param.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
}
}
注意 - 如果未明确设置,则会根据指定参数值的实际大小推断出大小。