How to generate random username and password in C#?

...................................
...............................source code................

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="randomusername.aspx.cs" Inherits="randomusername" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FF9999;
        }
        .style2
        {
            width: 79px;
        }
        .style3
        {
            width: 115px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server">
        <table class="style1">
            <tr>
                <td class="style2">
                    FirstName</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    LastName</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Mobile</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" Width="100px"
                        onclick="Button1_Click" />
                </td>
            </tr>
         
        </table>
        </asp:Panel>
        <asp:Panel ID="Panel2" runat="server">
        <table>
          <tr>
                <td class="style3">
                    Username&nbsp;</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Password</td>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
           
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Change Password..</asp:LinkButton>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
        </asp:Panel>
       
        <asp:Panel ID="Panel3" runat="server">
        <tr>
                <td class="style3">
                    Old Password</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    New Password</td>
                <td>
                    <asp:TextBox ID="TextBox5" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
               <tr>
                <td class="style3">
                    </td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" />                </td>
            </tr>
        </asp:Panel>
   
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    </form>
</body>
</html>
.......................end source code....................
......................code behind....................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
public partial class randomusername : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!IsPostBack)
            {
                Panel2.Visible = false;
                Panel3.Visible = false;
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert();
        Label1.Text = TextBox1.Text.Trim() + CreateRandomUsername(8);
        Label2.Text = CreateRandomPassword(8);
        Response.Write("<script>alert('[Your Username and Password is this')</script>");
        Panel2.Visible = true;
        Panel1.Visible = false;
        Panel3.Visible = false;
    }
    protected void insert()
    {
        string password = CreateRandomPassword(4);
        string fname = TextBox1.Text.Trim();
       // ViewState["fname"]=fname;
        string lname = TextBox2.Text.Trim();
       // ViewState["lname"]=lname;
        string str = "insert into random(firstname,lastname,mobile,password) values('" + fname + "','" + lname + "','" + TextBox3.Text + "','" + password + "')";
        cmd = new SqlCommand(str,con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        
    }
    public static string CreateRandomUsername(int UsernameLength)
    {


        string _allowedChars = "0123456789";
        Random randNum = new Random();
        char[] chars = new char[UsernameLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < UsernameLength; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }
    public static string CreateRandomPassword(int PasswordLength)
    {
       
        string _allowedChars = "0123456789abcdefghijkmnopqrstuvwxyz";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < PasswordLength; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }



    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Panel3.Visible = true;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string fname = TextBox1.Text.Trim();
        string str = "update random set password='" + TextBox5.Text + "' where firstname='" + fname + "'";
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script>alert('[Your Username and Password is updated..successfully')</script>");
        
    }
}
....................end code behind...............
Share:

No comments:

Post a Comment

Sunday, 16 December 2012

How to generate random username and password in C#?

...................................
...............................source code................

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="randomusername.aspx.cs" Inherits="randomusername" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FF9999;
        }
        .style2
        {
            width: 79px;
        }
        .style3
        {
            width: 115px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server">
        <table class="style1">
            <tr>
                <td class="style2">
                    FirstName</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    LastName</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Mobile</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" Width="100px"
                        onclick="Button1_Click" />
                </td>
            </tr>
         
        </table>
        </asp:Panel>
        <asp:Panel ID="Panel2" runat="server">
        <table>
          <tr>
                <td class="style3">
                    Username&nbsp;</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Password</td>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
           
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Change Password..</asp:LinkButton>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
        </asp:Panel>
       
        <asp:Panel ID="Panel3" runat="server">
        <tr>
                <td class="style3">
                    Old Password</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    New Password</td>
                <td>
                    <asp:TextBox ID="TextBox5" runat="server" Width="150px"></asp:TextBox>
                </td>
            </tr>
               <tr>
                <td class="style3">
                    </td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" />                </td>
            </tr>
        </asp:Panel>
   
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    </form>
</body>
</html>
.......................end source code....................
......................code behind....................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
public partial class randomusername : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!IsPostBack)
            {
                Panel2.Visible = false;
                Panel3.Visible = false;
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert();
        Label1.Text = TextBox1.Text.Trim() + CreateRandomUsername(8);
        Label2.Text = CreateRandomPassword(8);
        Response.Write("<script>alert('[Your Username and Password is this')</script>");
        Panel2.Visible = true;
        Panel1.Visible = false;
        Panel3.Visible = false;
    }
    protected void insert()
    {
        string password = CreateRandomPassword(4);
        string fname = TextBox1.Text.Trim();
       // ViewState["fname"]=fname;
        string lname = TextBox2.Text.Trim();
       // ViewState["lname"]=lname;
        string str = "insert into random(firstname,lastname,mobile,password) values('" + fname + "','" + lname + "','" + TextBox3.Text + "','" + password + "')";
        cmd = new SqlCommand(str,con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        
    }
    public static string CreateRandomUsername(int UsernameLength)
    {


        string _allowedChars = "0123456789";
        Random randNum = new Random();
        char[] chars = new char[UsernameLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < UsernameLength; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }
    public static string CreateRandomPassword(int PasswordLength)
    {
       
        string _allowedChars = "0123456789abcdefghijkmnopqrstuvwxyz";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < PasswordLength; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }



    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Panel3.Visible = true;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string fname = TextBox1.Text.Trim();
        string str = "update random set password='" + TextBox5.Text + "' where firstname='" + fname + "'";
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script>alert('[Your Username and Password is updated..successfully')</script>");
        
    }
}
....................end code behind...............

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive