本文实例讲述了C#简单生成随机密码的方法。分享给大家供大家参考,具体如下:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Windows.Forms; public partial class _Default : System.Web.UI.Page { public static int counter = 0; protected void Page_Load(object sender, EventArgs e) { } public static string MakePassword(string pwdchars, int pwdlen) { //判斷隨機密碼的長度 string tmpstr = ""; int iRandNum; if (pwdlen > pwdchars.Length) { return "长度过长!" + pwdchars.Length; } Random rnd = new Random(); for (int i = 0; i < pwdlen; i++) { iRandNum = rnd.Next(pwdchars.Length); tmpstr += pwdchars[iRandNum]; } return tmpstr; } protected void Button1_Click(object sender, EventArgs e) { int t1 = Convert.ToInt32(TextBox1.Text); if (t1 < 10 || t1 > 20 || t1 < 0) { MessageBox.Show("指定密碼失敗!"); }//判斷指定的密碼長度是否符合 else { string randomchars = "abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+|0123456789~!@#$%^&*()_+|ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|"; string password = MakePassword(randomchars, Convert.ToInt32(TextBox1.Text)); TextBox2.Text = password; }//產生隨機的密碼值 }//判斷輸入的密碼是否符合隨機產生的密碼 protected void Button2_Click(object sender, EventArgs e) { if (TextBox3.Text.Trim()==TextBox2.Text.Trim()) { MessageBox.Show("成功!"); Response.Write("您現在的密碼是:"+"<font color=red>"+TextBox3.Text+"</font>"); } else { MessageBox.Show("失败!"); } } protected void TextBox1_TextChanged(object sender, EventArgs e) { } }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页标题:C#简单生成随机密码的方法示例-创新互联
链接分享:https://www.cdcxhl.com/article16/csijdg.html
成都网站建设公司_创新互联,为您提供营销型网站建设、网站建设、品牌网站设计、建站公司、App开发、标签优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联