Login Page by using Login Control in ASP.Net

In this blog we will know how to Login page by using login control in ASP.Net.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Login_page_using_login_control._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title>Untitled Page</title
>

</
head>
<
body>
    <form id="form1" runat
="server">

   <div
>

     <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate"  

           DestinationPageUrl="~/Home.aspx" BackColor="#FFFBD6" BorderColor="#FFDFAD"

           BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
           Font-Size="0.8em" ForeColor="#333333" TextLayout
="TextOnTop">

         <TextBoxStyle Font-Size="0.8em"
/>

         <LoginButtonStyle BackColor="White" BorderColor="#CC9966" BorderStyle="Solid"

             BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000"
/>

         <InstructionTextStyle Font-Italic="True" ForeColor="Black"
/>

         <TitleTextStyle BackColor="#990000" Font-Bold="True" Font-Size="0.9em"

             ForeColor="White"
/>

     </asp:Login
>

    </div
>

    </form
>

</
body>
</
html>

.CS code

using System;

using System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
namespace
Login_page_using_login_control
{

    public partial class _Default : System.Web.UI.
Page

    {

        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand com;

        SqlDataAdapter sqlda;

        DataSet ds;

        string str;
 
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

        {
 
            str = "select * from users where UserName='" + Login1.UserName.ToString() + "' and password='" +
Login1.Password.ToString() + "'";

            using (SqlConnection conn = new SqlConnection(connStr))

            {

                ds = new DataSet();

                sqlda = new SqlDataAdapter();

                conn.Open();

                com = new SqlCommand(str, conn);

                com.ExecuteNonQuery();

                sqlda.SelectCommand = com;

                sqlda.Fill(ds);

                if (ds.Tables[0].Rows.Count > 0)

                {

                    conn.Close();

                    Response.Redirect("Home.aspx");

                }

               
else

                {

                    return;

                }

            }

        }

    }

}
 
Next Recommended Reading Login Page In MVC Using Entity Framework