Login page by using login control in Asp.net

Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 2675
We will know how to do login page using login control. Drag and drop a login control to a webpage and in Login1_Authenticate write the code as shown below to validate the user. If the user enters valid credentials then the control moves to Home.aspx page. If we enter wrong username and password it will display your login attempt was not successful. Please try again.

<%@ 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 runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<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" />

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();
com = new SqlCommand(str, conn);
sqlda.SelectCommand = com;
if (ds.Tables[0].Rows.Count > 0)

Comments or Responses

Posted by: Sheonarayan on: 8/21/2013 Level:HonoraryPlatinum | Status: [Administrator] | Points: 10
It would be great if you can describe your code snippet.


Login to post response