Description:- In This Example I will Show to How to Generate Random
Password and With Timer Controls We Will Generate Password So Based on Specific
Condition if We Have to Specify Password on Time limit Then We Have to Set
Timer to That Password. So for Particular Time that Password Re-Generated. And we
can Generate New Password to User. In My one of project I Have Done this
functionality.
Step 1: Now Create
Webpage and Design your webpage like below.
Step 2: Now Open Code behind File and Code for Generate Password and Maintain Time for Generated Password.
<div style="margin-left: 500px; margin-top: 50px; width: 436px;"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div> <asp:Timer ID="UpdateTimer" runat="server" Interval="1000" OnTick="UpdateTimer_Tick" /> <asp:UpdatePanel ID="TimedPanel" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" /> </Triggers> <ContentTemplate> <asp:Label runat="server" ID="lblTime" BackColor="#FFCCFF" BorderColor="White" Font-Bold="True" Font-Names="Arial Black" Font-Size="X-Large" ForeColor="#000099" ToolTip="Current Time" /> </ContentTemplate> </asp:UpdatePanel> </div> <div> <asp:Timer ID="Timer2" runat="server" OnTick="Timer2_Tick"> </asp:Timer> </div> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server" ViewStateMode="Enabled"> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" /> </Triggers> <ContentTemplate> <br /> <asp:Button ID="Btnsave" runat="server" CssClass="bt3dbuttons" OnClick="Btnsave_Click" Text="Submit" Width="77px" /> <br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br /> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <br /> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </ContentTemplate> </asp:UpdatePanel> <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel1" DynamicLayout="true" DisplayAfter="2000"> <ProgressTemplate> <img id="Img1" src="loading-bar.gif" runat="server" /> </ProgressTemplate> </asp:UpdateProgress> </div>
Step 2: Now Open Code behind File and Code for Generate Password and Maintain Time for Generated Password.
private System.Timers.Timer aTimer = new System.Timers.Timer(6000) { AutoReset = true }; protected void Timer2_Tick(object sender, EventArgs e) { // Generate(); //Btnsave.Enabled = false; aTimer = new System.Timers.Timer(6000); aTimer.Interval = 6000; double counter = aTimer.Interval; counter++; if (counter >= 6000) { Generate(); } } protected void Btnsave_Click(object sender, EventArgs e) { Generate(); Timer2.Enabled = true; Timer2.Interval = 5000; System.Threading.Thread.Sleep(4000); Label2.Text = "Password Generated at " + DateTime.Today.ToLongDateString() + " " + DateTime.Now.ToLongTimeString(); } public void Generate() { Label1.Text = ""; string allowedChars = ""; //allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"; allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"; allowedChars += "1,2,3,4,5,6,7,8,9,0"; //allowedChars += "!,@,#,$,%,^,&,*,(,),_,+,?,>,<"; char[] sep = { ',' }; string[] arr = allowedChars.Split(sep); string passwordString = ""; string temp = ""; Random rand = new Random(); for (int i = 0; i < Convert.ToInt32(10); i++) { temp = arr[rand.Next(0, arr.Length)]; passwordString += temp; } Label1.Text = passwordString; Label1.ForeColor = System.Drawing.Color.Blue; Label1.Font.Bold = true; Label1.Font.Size = 20; Label1.Font.Name = "Arial Black"; Label3.Text = "Update Automatically After 5 Second."; Btnsave.Enabled = false; } protected void UpdateTimer_Tick(object sender, EventArgs e) { lblTime.Text = DateTime.Now.ToString("hh:mm:ss"); }
Step 3: Now open your Webpage See How it is Works.
Step 4: After 5
Second Password will Generate and another 5 second Re-Generate.
Step 5: if you don’t click
on Button then it will Automatically Generate Password with Timer.
Thanks for comments.....