To Generate Random OTP in Asp.net Open Your aspx Page and Write the Following Code
<center>
<div>
Enter Number of Characters: <asp:TextBox ID="TxtCharacters" runat="server"/>
<asp:Button ID="BtnGenerateChar" Text="Generate" runat="server"
onclick="BtnGenerateChar_Click" /><br />
<asp:Label ID="lblresult" runat="server" ForeColor="Blue" />
</div>
</center>
Now Add following Namespaces in Code behind
using System;
using System.Web;
After Completion of Adding Namespaces You Need to Write the Code like as Shown below
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnGenerateChar_Click(object sender, EventArgs e)
{
// Declare Array string to Generate Random string with Combination of Small,Capital letters Numbers and Special Characters
char[] charArr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%&".ToCharArray();
string stringRandom = string.Empty;
Random objran = new Random();
int noofcharacters = Convert.ToInt32(TxtCharacters.Text);
for (int i = 0; i < noofcharacters; i++)
{
//It will not allow Repetation of Characters
int pos = objran.Next(1, charArr.Length);
if (!stringRandom.Contains(charArr.GetValue(pos).ToString()))
stringRandom += charArr.GetValue(pos);
else
i--;
}
lblresult.Text = stringRandom;
}
No comments:
Post a Comment