1
public
static
string
MakePassword(
string
pwdchars,
int
pwdlen)
2 {
3 string tmpstr = "";
4 int iRandNum;
5 Random rnd = new Random();
6 for(int i=0;i<pwdlen;i++)
7 {
8 iRandNum = rnd.Next(pwdchars.Length);
9 tmpstr += pwdchars[iRandNum];
10 }
11 return tmpstr;
12 }
13
2 {
3 string tmpstr = "";
4 int iRandNum;
5 Random rnd = new Random();
6 for(int i=0;i<pwdlen;i++)
7 {
8 iRandNum = rnd.Next(pwdchars.Length);
9 tmpstr += pwdchars[iRandNum];
10 }
11 return tmpstr;
12 }
13
其中pwdchars 为密码中出现的字符,pwdlen为密码的长度
2、后台的代码:
1
private
void
Page_Load(
object
sender, System.EventArgs e)
2 {
3 if(!IsPostBack)
4 {
5 string randomchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
6
7 string password = Class1.MakePassword(randomchars, 10);
8 Response.Write(password);
9 }
10 // 在此处放置用户代码以初始化页面
11 }
12
13

2 {
3 if(!IsPostBack)
4 {
5 string randomchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
6
7 string password = Class1.MakePassword(randomchars, 10);
8 Response.Write(password);
9 }
10 // 在此处放置用户代码以初始化页面
11 }
12

13
