Create a Login page - ASP.NET Program

Create an ASP.NET Login page similar to the image given below.

asp.net login page

Store the User ID and password in variables. After matching the entered credentials, user should get navigated to the welcome.aspx page with message “Welcome Mr./Mrs____”.

Answer:

Introduction:
Here, we create a ASP.NET login page which can accept username and password from the user and validate it.

If username and password match, the login page redirects to welcome.aspx page and displays the welcome message, else it throws an error message.

The username and password are stored in the local variable in page. We will use the VB language to develop this application.

Login-page.aspx

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
          <title></title>
   </head>
   <body>
       <script language="VB" runat="server">
            
             Private Sub Page_Load(sender As Object, e As EventArgs)
                If Request.RequestType = "POST" Then
                    Dim userid As String = Request.Form("txtUserid")
                    Dim password As String = Request.Form("txtpwd")

                    If userid = "matrix" And password = "12345" Then
                        Response.Redirect("welcome.aspx")
                    Else
                        lblStatus.Text = "Invalid userid or password!"
                    End If

                End If
            End Sub

         </script>
    <form id="f1" method="post" runat="server">
            <fieldset style="width:280px; background-color:aqua">
                 <legend>Login</legend>
                 <table>
                       <tr>
                              <td>User ID:<font color="#cc0000">*</font></td>
                              <td><asp:TextBox ID="txtUserid" runat="server"></asp:TextBox><br /> </td>
                       </tr>
                       <tr>
                              <td>Password:<font color="#cc0000">*</font></td>
                              <td><asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox><br /></td>
                      </tr>
                      <tr>
                             <td> </td>
                             <td><asp:Button ID="btnLogin" runat="server" Text="Login" /></td>
                      </tr>
                      <tr>
                             <td> </td>
                             <td><asp:Label ID="lblStatus" runat="server" Text=""></asp:Label></td>
                     </tr>
                 </table>
            </fieldset>   
       </form>
   </body>
</html>


Output:

Login Screen

asp.net login page

Wrong userid or password

wrong userid password

Correct userid and password (welcome page)

welcome msg