大家好,今天跟大家學習寫一個亂數密碼產生器
public ActionResult Index()
{ //密碼亂數
string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
int passwordLength = 6;//密碼長度
char[] chars = new char[passwordLength];
Random rd = new Random();
for (int i = 0; i < passwordLength; i++)
{
//allowedChars -> 這個String ,隨機取得一個字,丟給chars[i]
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
string password = new string(chars);
ViewBag.password = password;
return View();
}
2.View
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ViewBag.password
參考網址:https://madskristensen.net/blog/generate-random-password-in-c/