Write ASP.NET program for User Login

Write ASP.NET program for User Login. Use database concept and if User Name and Password get matched, then redirect user to the Home page else to the Login page.

Answer:

Introduction:
In this application, we use database concept to check if the username and password are correct or not. If username and password get matched, then user gets redirected to the home page otherwise to the login page.

Steps to develop the login page.

1. Create a table “UserLogIn”, with two columns such as username and password.

create table UserLogin (username varchar(20), password varchar(20))

2. Create a Stored procedure “Validate-User” to check if the username and password match or not.

CREATE PROCEDURE [dbo].[Validate_User]
      @Username VARCHAR(20),
      @Password VARCHAR(20),
              @Msg varchar(200) out
AS
set @Msg = ''

SET NOCOUNT ON

IF EXISTS(SELECT * FROM UserLogIn WHERE username = @username AND password = @password)
   set @Msg = 'Valid password'
ELSE
   set @Msg = 'Invalid username or password'


3. Develop the web page in VB.NET

Login.aspx

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
         <title>LogIn Page</title>
    </head>
   <body>
           <%@ Import Namespace="System.Data.SqlClient" %>
           <%@ Import Namespace="System.Data" %>
           <%@ Import Namespace="System" %>
           <%@ Import Namespace=" System.Configuration" %>

          <script language="VB" runat="server">

            Protected Sub ValidateUser(sender As Object, e As EventArgs)
                Using oConn As New SqlConnection

                     Dim cmd As New SqlCommand
                     Dim pIn As New SqlParameter
                     Dim pOut As New SqlParameter
                     oConn.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
                     oConn.Open()

                     cmd = New SqlCommand("Validate_User", oConn)
                     cmd.CommandType = CommandType.StoredProcedure
                     pIn = cmd.Parameters.Add("@Username", SqlDbType.VarChar, 20)
                     pIn.Value = txtUserid.Text
                     pIn = cmd.Parameters.Add("@Password", SqlDbType.VarChar, 20)
                     pIn.Value = txtPwd.Text

                     Dim Msg As New SqlParameter("@Msg", SqlDbType.VarChar, 200)
                     Msg.Direction = ParameterDirection.Output

                     cmd.Parameters.Add(Msg)
                     cmd.ExecuteNonQuery()

                     lblStatus.Visible = True
                     lblStatus.Enabled = True
                     lblStatus.Text = Msg.Value
                     If Msg.Value = "Valid password" Then
                          Response.Redirect("welcome.aspx")
                    End If

               End Using
        End Sub

    </script>
    <body>
         <form id="form1" runat="server">
   
             <fieldset style="width:280px; background-color:aqua">
                   <asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
                   <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" OnClick ="ValidateUser" /></td>
                         </tr>
                    </table>
              </fieldset>

           </form>
      </body>
</html>


Output:

1. Username or password are not matched

username password not matched

2. Username and password are matched

username password matched