Sunday 30 August 2015

Generate One Time Password (OTP) in Asp.net using C#

// HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OPT Password in Asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Number of OPT Digit: <asp:TextBox ID="txtCharacters" runat="server"/>
<asp:Button ID="btnGenerate" Text="Generate" runat="server"
onclick="btnGenerate_Click" /><br />
<asp:Label ID="lblResult" runat="server" ForeColor="Red" />
</div>
</form>
</body>
</html>


//CS Page

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnGenerate_Click(object sender, EventArgs e)
{
// declare array string to generate random string with combination of small,capital letters and numbers
char[] charArr ="0123456789abcdefghijklmnopqrstuvwxyz@#$<>ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
string strrandom = string.Empty;
Random ran = new Random();
int noofcharacters = Convert.ToInt32(txtCharacters.Text);
for (int i = 0; i < noofcharacters; i++)
{
//It will not allow Repetation of Characters
int pos = ran .Next(1, charArr.Length);
if (!strrandom.Contains(charArr.GetValue(pos).ToString()))
strrandom += charArr.GetValue(pos);
else
i--;
}
lblResult.Text = strrandom;
}

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...