c#.net怎么保存用户名和密码

Gavin_Y 2008-08-02 09:49:17
c#.net怎么保存用户名和密码,点击保存密码下次登陆可以直接进去
...全文
472 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
umbrella_yang 2008-08-03
  • 打赏
  • 举报
回复
如果是登陆的话,请把用户的个人设置序列化为文件(用户个人配置文件 - Profile)
qjlsharp 2008-08-03
  • 打赏
  • 举报
回复
这东西一般是保存在数据库里比较安全吧
vshirleyzhxl 2008-08-03
  • 打赏
  • 举报
回复
winform可以建数据表来存储用户名和密码信息的表,
然后建议把默认用户用配置文件来记录,在窗体Load时读出来并自动填入,用户登陆时回车就OK了。

下面分别给两个代码吧。
(一)保存用户名和密码到数据库
记得在引用部分加上:
using System.Data.SqlClient;
private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text

string userPswd = textBox2.Text

string constr = "Server=(Local);User Id=Sa;Pwd=;DataBase=HSSN";
SqlConnection con = new SqlConnection(constr);
con.Open();
string instr;
instr = "insert into userinfo(name,pswd) values (" + "'" + userName + "'" + "," + "'" + userPswd + "'" + ")";
SqlCommand cmd = new SqlCommand(instr, con);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

con.Close();
}

(二)下面是一个Iin文件读写的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace IniOperator
{
public class INIClass
{
public string inipath;

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

public INIClass(string INIPath)
{
inipath = INIPath;
}
///
/// 写入INI文件
///
/// 项目名称(如 [TypeName] )
/// 键
/// 值
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
///
/// 读出INI文件
///
/// 项目名称(如 [TypeName] )
/// 键
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
///
/// 验证文件是否存在
///
/// 布尔值
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
(三、)读用上面的类读定Ini文件
private INIClass inic;
inic = new INIClass("写上你的文件名");
//写入
if (inic.ExistINIFile())
{
try
{
inic.IniWriteValue(textBox2.Text, textBox3.Text, textBox4.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//写入ini文件
try
{
textBox4.Text = inic.IniReadValue(textBox2.Text, textBox3.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Gavin_Y 2008-08-02
  • 打赏
  • 举报
回复
那把数据库路保存到哪里
x55859000 2008-08-02
  • 打赏
  • 举报
回复
........webfrom需要流

winfrom需要数据库...
Gavin_Y 2008-08-02
  • 打赏
  • 举报
回复
那要用到流吗
aimeast 2008-08-02
  • 打赏
  • 举报
回复
存数据库路就可以了。至于数据库是什么格式的,那就随便了。纯文本的也行。
Gavin_Y 2008-08-02
  • 打赏
  • 举报
回复
winform的
wulaiwangyi 2008-08-02
  • 打赏
  • 举报
回复
就是啊楼主你是要winform还是webform,

winform就把登录名和密码写到数据库
webform就写到cookies里面!

cookies方面的这里也许有用
http://www.bitscn.com/dotnet/c/200803/130948.html
aimeast 2008-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuxiuming 的回复:]
用cookie实现吧
[/Quote]你是要winform还是webform啊?
aimeast 2008-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuxiuming 的回复:]
用cookie实现吧
[/Quote]
正解!所有的保存密码都是用的cookies
Gavin_Y 2008-08-02
  • 打赏
  • 举报
回复
详细一点的,cookies我不怎么会用
liuxiuming 2008-08-02
  • 打赏
  • 举报
回复
用cookie实现吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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