Notice: A non well formed numeric value encountered in C:\ClientSites\reviewhostingasp.net\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\reviewhostingasp.net\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\reviewhostingasp.net\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\reviewhostingasp.net\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\reviewhostingasp.net\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\reviewhostingasp.net\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
ASP.NET is an open-source server-side web application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services.
It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft’s Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime (CLR), allowing programmers to write ASP.NET code using any supported .NET language. The ASP.NET SOAP extension framework allows ASP.NET components to process SOAP messages.
ASP.NET is in the process of being re-implemented as a modern and modular web framework, together with other frameworks like Entity Framework. The new framework will make use of the new open-source .NET Compiler Platform (code-name “Roslyn”) and be cross platform. ASP.NET MVC, ASP.NET Web API, and ASP.NET Web Pages (a platform using only Razor pages) will merge into a unified MVC 6. The project is called ASP.NET Core.
In this article I am going to share how to generate or create random unique alphanumeric one time password (OTP) in Asp.Net using both C# and VB.
Nowadays one time password(OTP) system is widely used by banks and other firms to authenticate users while making online transactions and other similar activities. Here I have created a function to generate OTP which is combination of numbers, lower and uppercase letters and special characters or symbols.
I have added only few special characters while generating OTP but you can add as many as special characters as required to make it more unique. It generates the OTP of the length we specify. E.g. if we pass 10 from the textbox, it will generate OTP of length 10. OTP can be generated of any length as per requirement. Generated OTP can be sent through Email or SMS to user for authentication.
Let’s create a sample web page for demonstration purpose
1 2 3 4 5 | Enter Number of Characters: <asp:TextBox ID="txtOTP" runat="server" /> <asp:Button ID="btnGenerateOTP" Text="Generate" runat="server" OnClick="btnGenerateOTP_Click" /><br /> <asp:Label ID="lblOTP" runat="server" ForeColor="blue" /> |
Asp.Net C# Code to generate One Time Password(OTP)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | protected void btnGenerateOTP_Click(object sender, EventArgs e) { lblOTP.Text = GenerateOTP(Convert.ToInt32(txtOTP.Text.Trim())); } public string GenerateOTP(int Length) { string _allowedChars = "#@$&*abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"; Random randNum = new Random(); char[] chars = new char[Length]; for (int i = 0; i < Length; i++) { chars[i] = _allowedChars[Convert.ToInt32((_allowedChars.Length - 1) * randNum.NextDouble())]; } return new string(chars); } |
Asp.Net VB Code to generate One Time Password(OTP)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Protected Sub btnGenerateOTP_Click(sender As Object, e As EventArgs) Handles btnGenerateOTP.Click lblOTP.Text = GenerateOTP(Convert.ToInt32(txtOTP.Text.Trim())) End Sub Public Function GenerateOTP(Length As Integer) As String Dim _allowedChars As String = "#@$&*abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789" Dim randNum As New Random() Dim chars As Char() = New Char(Length - 1) {} For i As Integer = 0 To Length - 1 chars(i) = _allowedChars(Convert.ToInt32((_allowedChars.Length - 1) * randNum.NextDouble())) Next Return New String(chars) End Function |