小错误我无法弄清楚
本文关键字:弄清楚 错误 | 更新日期: 2023-09-27 18:30:33
有一个小错误,为了我的爱,我不知道为什么:
错误 1 名称"电子邮件"在当前上下文中不存在 C:''Users''Janco''Desktop''New folder''Espresso Studio's C''Espresso Studio's C''Espresso Studio's C''Form1.cs 135 69 Espresso Studio's C
错误位于:
public string HashPass(string password)
{
SHA256 sha = new SHA256CryptoServiceProvider();
//compute hash from the bytes of text
sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email)); // Error occurs here at email.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
namespace Espresso_Studio_s_C
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void loginsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.loginsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.loginsDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'loginsDataSet.Logins' table. You can move, or remove it, as needed.
this.loginsTableAdapter.Fill(this.loginsDataSet.Logins);
}
private void registerButton_Click(object sender, EventArgs e)
{
AddUser(registerUserName.Text, registerPassword.Text, registerConfirmPassword.Text, registerEmail.Text);
}
private void AddUser(string username, string password, string confirmPass, string email)
{
//Local variables to hold values
string smtpEmail = smtpUserNameTextBox.Text;
string smtpPassword = smtpPasswordTextBox.Text;
int smtpPort = (int)smtpPortNumericUD.Value;
string smtpAddress = smtpAddressTextBox.Text;
//Regex for making sure Email is valid
Regex regex = new Regex(@"^(['w'.'-]+)@(['w'-]+)(('.('w){2,3})+)$");
Match match = regex.Match(email);
//Loop through Logins Table
foreach (DataRow row in loginsDataSet.Logins)
{
//And look for matching usernames
if (row.ItemArray[0].Equals(username))
{
//If one is found, show message:
MessageBox.Show("Username already exists");
return;
}
}
//Confirm pass must equal password.
if (password != confirmPass)
{
MessageBox.Show("Passwords do not match");
}
//Password must be at least 8 characters long
else if (password.Length < 8)
{
MessageBox.Show("Password must be at least 8 characters long");
}
//If email is NOT valid
else if (!match.Success)
{
MessageBox.Show("Invalid Email");
}
//If there is no username
else if (username == null)
{
MessageBox.Show("Must have Username");
}
//If all is well, create the new user!
else
{
loginsDataSet.LoginsRow newUserRow = loginsDataSet.Logins.NewLoginsRow();
string EncryptedPass = HashPass(password);
newUserRow.Username = username;
newUserRow.Password = EncryptedPass;
newUserRow.Email = email;
loginsDataSet.Logins.Rows.Add(newUserRow);
registerUserName.Text = String.Empty;
registerPassword.Text = String.Empty;
registerConfirmPassword.Text = String.Empty;
registerEmail.Text = String.Empty;
MessageBox.Show("Thank you for Registering!");
if (String.IsNullOrWhiteSpace(smtpEmail) || String.IsNullOrWhiteSpace(smtpPassword) || String.IsNullOrWhiteSpace(smtpAddress) || smtpPort <= 0)
{
MessageBox.Show("Email configuration is not set up correctly! 'nCannot sent email!");
}
else
{
SendMessage(email.ToString(), username.ToString(), password.ToString());
}
}
}
public string HashPass(string password)
{
SHA256 sha = new SHA256CryptoServiceProvider();
//compute hash from the bytes of text
sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email)); // Error occurs here
//get hash result after compute it
byte[] result = sha.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
//change it into 2 hexadecimal digits
//for each byte
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
}
public void SendMessage(string ToAddress, string ToName, string password)
{
var client = new SmtpClient(smtpAddressTextBox.Text, (int)smtpPortNumericUD.Value)
{
Credentials = new NetworkCredential(smtpUserNameTextBox.Text, smtpPasswordTextBox.Text),
EnableSsl = true
};
client.Send(smtpUserNameTextBox.Text, ToAddress, "Thank You!", "Thank you for registering with us today! 'n Your username/passwords are: 'n 'nUsername: "
+ ToName.ToString() + "'nPassword: " + password.ToString());
}
private void loginButton_Click(object sender, EventArgs e)
{
//Locals to hold values
string username = loginUserName.Text;
string password = HashPass(loginPassword.Text);
//Loop through database
foreach (DataRow row in loginsDataSet.Logins)
{
//And search for Username and Pass that match
if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))
{
loginUserName.Text = String.Empty;
loginPassword.Text = String.Empty;
MessageBox.Show("Login Success");
break;
}
//If not, then show this message.
else
{
MessageBox.Show("Username/Password incorrect");
break;
}
}
}
}
}
AddUser 接收变量电子邮件的方法。这是一个参数,因此它是一个局部变量,仅在方法AddUser中可见。没有其他方法可以看到或使用它,除非您将变量作为参数传递或将其分配给全局变量(不推荐)
private void AddUser(string username, string password, string confirmPass, string email)
所以你需要将该变量传递给 HashPass 方法
.... in AddUser .....
else
{
loginsDataSet.LoginsRow newUserRow = loginsDataSet.Logins.NewLoginsRow();
string EncryptedPass = HashPass(password, email);
....
}
...
public string HashPass(string password, string anEmailAddress)
{
SHA256 sha = new SHA256CryptoServiceProvider();
//compute hash from the bytes of text
sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + anEmailAddress));
....
}