国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#簡單生成隨機密碼的方法示例

2019-10-29 21:09:14
字體:
來源:轉載
供稿:網友

本文實例講述了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)  {  }}

HTML代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server">  <title>無標題頁</title></head><body bgcolor="#66ffcc">  <form id="form1" runat="server" >  <div>         <br />    <br />    <br />    <br />    <br />    <br />    <table bgcolor=SpringGreen>      <tr>        <td width="100px">    <asp:Label ID="Label1" runat="server" Text="指定密碼長度(10~20之間)" Width="129px"></asp:Label></td>        <td width="159px">    <asp:TextBox ID="TextBox1" runat="server" Width="39px" OnTextChanged="TextBox1_TextChanged">10</asp:TextBox>    <asp:TextBox ID="TextBox2" runat="server" Width="195px" ReadOnly="true"></asp:TextBox></td>        <td width="103px">    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="產生密碼" /></td>        <td width="100px">        </td>      </tr>      <tr>        <td width="100px">    <asp:Label ID="Label2" runat="server" Text="輸入產生的密碼"></asp:Label></td>        <td width="159px">          <asp:TextBox ID="TextBox3" runat="server" Width="261px" TextMode="Password"></asp:TextBox></td>        <td width="103px">    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="驗證" /></td>        <td width="100px">        </td>      </tr>    </table>    <br />    <br />    <br />    </div>  </form></body></html>

希望本文所述對大家C#程序設計有所幫助。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 当阳市| 安国市| 望江县| 西贡区| 松潘县| 怀化市| 绥德县| 锡林浩特市| 德昌县| 乡城县| 岱山县| 宜君县| 铜陵市| 华亭县| 马鞍山市| 玛曲县| 赤峰市| 淅川县| 黑水县| 抚顺市| 安泽县| 会宁县| 荔浦县| 正阳县| 耿马| 西平县| 社会| 哈尔滨市| 阿拉善左旗| 陆河县| 靖远县| 扶风县| 盈江县| 额敏县| 白河县| 济宁市| 云林县| 南阳市| 邛崃市| 河北区| 南靖县|