/
0
Password Generator in ASP.NET
This article illustrates how to create a simple password generator.
Create a new website in VS and add the following code that designs our aspx page.
Create a new website in VS and add the following code that designs our aspx page.
<%@ 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 runat="server"> <title>Password Generator</title> <script type="text/javascript"> //Javascript function to allow only numeric values in the text box. function isNumeric(keyCode) { if (keyCode == 16) isShift = true; return ((keyCode >= 48 && keyCode <= 57 || keyCode == 8 || (keyCode >= 96 && keyCode <= 105)) && isShift == false); } </script> </head> <body> <form id="form1" runat="server"> <div> <b>Password Generator</b><br /> <br /> <table cellpadding="2"> <tr> <td> Password Length </td> <td> <asp:TextBox ID="txtPassLength" runat="server" Width="180px" onkeydown = "return isNumeric(event.keyCode);"></asp:TextBox> </td> <td> <asp:Button ID="btnGeneratePassword" runat="server" Text="Generate Password" OnClick="btnGeneratePassword_Click" /> </td> </tr> </table> <asp:Label ID="lblPassword" runat="server" Text="Password" ForeColor="#993300"></asp:Label> </div> </form> </body> </html>And it’s code behind as follows
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { private char[] _passCharactors = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*".ToCharArray(); private int _passLength = 0; private string _password; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { lblPassword.Visible = false; } txtPassLength.Attributes.Add("onkeydown", "return isNumeric(event.keyCode);"); } protected void btnGeneratePassword_Click(object sender, EventArgs e) { Random rnd = new Random(); if (!string.IsNullOrEmpty(txtPassLength.Text)) { _passLength = Convert.ToInt32(txtPassLength.Text); if (_passLength > 0) { for (int i = 0; i < _passLength; i++) { int randNumber = rnd.Next(0, _passCharactors.Length - 1); _password += _passCharactors[randNumber]; } lblPassword.Visible = true; lblPassword.Text = "Your Password : " + _password; } } else { lblPassword.Visible = true; lblPassword.Text = "Please enter password length."; } } }
Enjoy this article?
Tagged as:
ASP.NET,
Password Generator
0
comments
Comments (0)
Subscribe to:
Post Comments (Atom)
Translate
Labels
- ASP.NET (4)
- Cookies (1)
- FileUpload Control (1)
- Office 2010 Errors (1)
- Password Generator (1)
- SharePoint 2010 (4)
Followers
About Me
Powered by Blogger.