VS里的window做一个登录界面,那记住密码的功能要怎么写?代码在下面,但一执行就显示错误

weixin_42811411 2018-08-08 01:09:29
namespace 测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//要先将User类先设为可以序列化(即在类的前面加[Serializable])
[Serializable]
public class User
{
private string userName;
public string Username
{
get { return userName; }
set { userName = value; }
}

private string passWord;
public string Password
{
get { return passWord; }
set { passWord = value; }
}
}
private void Form1_Load(object sender, EventArgs e)
{
// 读取配置文件寻找记住的用户名和密码
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);

if (fs.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();
users = bf.Deserialize(fs) as Dictionary<string, User>;
foreach (User user in users.Values)
{
this.UserName.Items.Add(user.Username);
}
if (this.UserName.Items.Count > 0)
{
this.UserName.SelectedIndex = this.UserName.Items.Count - 1;

for (int i = 0; i < users.Count; i++)
{
if (this.UserName.Text != "")
{
if (users.ContainsKey(this.UserName.Text))
{
this.PassWord.Text = users[this.UserName.Text].Password;
this.RemeberPassword.Checked = true;
}
}
}
}
}
fs.Close();
}


private void button1_Click(object sender, EventArgs e)
{
string constr = @"Data Source=DESKTOP-NAHV8AN;Initial Catalog=master;Integrated Security=True";
string sql = string.Format("select count(*) from users where Username='{0}' and Password='{1}'", UserName, PassWord);
//string sql = "select count(*) from login where username='" + name + "' and pwd='" + pwd + "'";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
object ob = cmd.ExecuteScalar();
con.Close();
if (Convert.ToInt32(ob) > 0)
{
MessageBox.Show("登录成功");
Form2 f2 = new Form2();

f2.Show();
this.Hide();
}
else MessageBox.Show("登录失败,用户名或密码错误!");


string username = this.UserName.Text.Trim();
string password = this.PassWord.Text.Trim();
User user = new User();
FileStream fs = new FileStream("data.txt", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
user.Username = username;
if (this.RemeberPassword.Checked) // 如果单击了记住密码的功能
{ // 在文件中保存密码
user.Password = password;
}
else
{ // 不在文件中保存密码
user.Password = "";
}

// 选在集合中是否存在用户名


if (users.ContainsKey(user.Username))
{
users.Remove(user.Username);
}
users.Add(user.Username, user);
//要先将User类先设为可以序列化(即在类的前面加[Serializable])
bf.Serialize(fs, users);
//user.Password = this.PassWord.Text;
fs.Close();
}

public Dictionary<string, User> users { get; set; }

private void UserName_SelectedIndexChanged(object sender, EventArgs e)
{
// 首先读取记住密码的配置文件
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);

if (fs.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();

users = bf.Deserialize(fs) as Dictionary<string, User>;

for (int i = 0; i < users.Count; i++)
{
if (this.UserName.Text != "")
{
if (users.ContainsKey(UserName.Text) && users[UserName.Text].Password != "")
{
this.PassWord.Text = users[UserName.Text].Password;
this.RemeberPassword.Checked = true;
}
else
{
this.PassWord.Text = "";
this.RemeberPassword.Checked = false;
}
}
}
}

fs.Close();

}
}
}
...全文
830 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_42811411 2018-08-29
  • 打赏
  • 举报
回复
那要怎么办,
大然然 2018-08-24
  • 打赏
  • 举报
回复
也就是在这里的时候
if (users.ContainsKey(user.Username))
{
users.Remove(user.Username);
}
大然然 2018-08-24
  • 打赏
  • 举报
回复
当你的程序第一次运行的时候,fs 是等于0的,也就是永远不会进入这个判断,那么 Dictionary<string, User> users这个字典也就是永远为空,所以你取里面的值就报错了
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);

if (fs.Length > 0)
qq_38660532 2018-08-23
  • 打赏
  • 举报
回复
简单一点:将记住的用户名,密码直接保存到配置文件中(*.ini)不就得了。
雨何方 2018-08-23
  • 打赏
  • 举报
回复
简单一点:将记住的用户名,密码直接保存到配置文件中(*.ini)不就得了。
weixin_42811411 2018-08-10
  • 打赏
  • 举报
回复
明文保存要怎么弄,不用谢语句吗?老师没教过,只能自己慢慢查,暑假在家也没带书回来
吹风的兔子 2018-08-09
  • 打赏
  • 举报
回复
对于这种 可有可无 不影响程序主逻辑 的 附属功能 —— 请先无脑加上 try-catch 。
  • 打赏
  • 举报
回复
引用 3 楼 weixin_42811411 的回复:
还有,我data.txt文件里要写什么,怎么写

看你自己的设计思路,有人直接把用户名密码明文保存,有人用des或aes加密密文保存,有人用服务器下发一个login的状态uuid保存后对这个id,你自己系统怎么设计怎么保存,就是保存一个用户名+口令的组合,至于这个组合的详细情况是你自己根据业务实际情况变更的。
  • 打赏
  • 举报
回复
引用 2 楼 weixin_42811411 的回复:
if (users.ContainsKey(user.Username))这一句出错,提示未将对象引用设置到对象的实例。而且每次都是用户米或密码错误

在判断ContainsKey之前先判断users非空
weixin_42811411 2018-08-09
  • 打赏
  • 举报
回复
还有,我data.txt文件里要写什么,怎么写
weixin_42811411 2018-08-09
  • 打赏
  • 举报
回复
if (users.ContainsKey(user.Username))这一句出错,提示未将对象引用设置到对象的实例。而且每次都是用户米或密码错误
萨拉嘿 2018-08-08
  • 打赏
  • 举报
回复
报的什么错呀,具体内容说一下?简略看了下代码,
users = bf.Deserialize(fs) as Dictionary<string, User>;
foreach (User user in users.Values)
{
this.UserName.Items.Add(user.Username);
}

反序列化会成功么?users为null的情况下users.Values会报错的

17,741

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧