如何在本地数据库中保存变量值,这样当我再次启动程序时,我可以再次获取变量的值。
我希望允许用户在重新启动程序时更改其PIN码并使用新的PIN码。
这是一个Windows窗体应用程序,我有一个按钮来更改用于允许用户打开程序的密码
private void button17_Click(object sender, EventArgs e)
{
/*
The change PIN number has a problem and the pin number needs to be on a database or a text file in order to be updated correctly
*/
if (message.TextLength != 0 && message.TextLength < 5)
{
message.Text = "Enter Your Current 4 digits PIN ";
if (Convert.ToInt32(message.Text) == currentpinn)
{
message.Text = "Enter Your New 4 digits PIN ";
currentpinn = Convert.ToInt32(message.Text);
message.Text = " Your PIN has changed successfully ";
}
message.Text = "The PIN you have entered is not correct please try again! ";
}
else
{
message.Text = "Please enter a valied PIN ";
}
}
答案 0 :(得分:1)
通常,您需要做的是拥有密码的系统:
存储密码
验证密码
private void buttonCreate_Click(object sender, EventArgs e)
{
using (var d = new GetNewPasswordDialog())
{
d.ShowDialog(this);
if (d.DialogResult == DialogResult.OK)
{
var newPassword = d.Password;
var salt = CreateRandomSalt(33);
var hash = GeneratePasswordHash(newPassword, salt);
System.Diagnostics.Debug.WriteLine(hash);
System.Diagnostics.Debug.WriteLine(salt);
// save the hash & salt
// using application config as an example
Properties.Settings.Default.Hash = hash;
Properties.Settings.Default.Salt = salt;
Properties.Settings.Default.Save();
}
}
}
private void buttonVerify_Click(object sender, EventArgs e)
{
// The change PIN number has a problem and the pin number
// needs to be on a database or a text file in order to be updated correctly
if (textBox1.TextLength != 0 && textBox1.TextLength < 5)
{
message.Text = Properties.Resources.EnterPin;
// read in the current password hash and salt from storage
var savedSalt = Properties.Settings.Default.Salt;
var savedHash = Properties.Settings.Default.Hash;
// get the pin number that was entered and generate a hash using the saved salt
var currentPIN = textBox1.Text.Trim();
var currentHash = GeneratePasswordHash(currentPIN, savedSalt);
if (string.Compare(savedHash, currentHash) == 0)
{
message.Text = "Your PIN is correct";
// TODO: Allow user to change PIN
buttonCreate.Enabled = true;
}
else
{
message.Text = "The PIN you have entered is not correct please try again! ";
}
}
else
{
message.Text = "Please enter a valid PIN";
}
}
private string CreateRandomSalt(int length)
{
// Generate a random salt
var salt = new byte[length];
using (var csprng = new RNGCryptoServiceProvider())
{
csprng.GetBytes(salt);
}
return Convert.ToBase64String(salt);
}
private string GeneratePasswordHash(string password, string salt)
{
using (var pbkdf2 = new Rfc2898DeriveBytes(password, Convert.FromBase64String(salt)))
{
pbkdf2.IterationCount = 1000;
var hash = pbkdf2.GetBytes(50);
return Convert.ToBase64String(hash);
}
}
答案 1 :(得分:1)
Settings.settings在磁盘上存储首选项。当程序退出时将设置写入磁盘,然后在程序再次启动时将其读回,这很麻烦。我们可以使用Settings.settings文件。
双击Visual Studio文件资源管理器中的Settings.settings文件,您应该看到一个设置表。接下来,在最左侧的列中键入变量的名称。在当前情况下,我使用了PinNumber
。确保范围是用户。
然后在Code Behind中你可以像这样使用那个变量
//Put all validations here like 4 digit, IsNumber etc...
Properties.Settings.Default.PinNumber = CurrentPin; // Assign Value
Properties.Settings.Default.Save(); // Saving the Value
CurrentPin = Properties.Settings.Default.PinNumber; //Getting the last value