This Article explains on how to create a login page using C# and will be helpful for the beginners learning ASP.NET
Introduction
This Article explains on how to create a login page using C# and will be helpful for the beginners learning ASP.NET
Objective
Create a login page in ASP.NET (C#) using stored procedure
Using the code
1.CREATE DATABASE AND TABLE
****************************
USE [dbname]
GO
/****** Object: Table [dbo].[Tbl_User] Script Date: 11/19/2012 10:20:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Tbl_User](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[UserName] [varchar](50) NULL,
[Password] [varchar](50) NULL,
[Gender] [varchar](1) NULL,
[City] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
2.CREATE STORED PROCEDURE
**************************
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: RAJARAJAN
-- Description: LOGIN CHECK
-- =============================================
CREATE PROCEDURE [dbo].[Sp_loginCheck]
@UserName varchar(50),
@Password varchar(50)
as
BEGIN
SET NOCOUNT ON;
declare @log as varchar(50)
select @log=COUNT(id) from Tbl_User where UserName=@UserName AND [Password]=@Password
if(@log>0)
begin
select id,Name from Tbl_User where UserName=@UserName AND Password=@Password
end
else
begin
raiserror('Login Failed',16,1)
end
END
3.loginpage.aspx
******************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_page.aspx.cs" Inherits="_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></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<table class="style1" bgcolor="#99CCFF">
<tr>
<td align="center">
<br />
<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#333333" Height="148px"
onauthenticate="Login1_Authenticate" Width="295px">
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" />
</asp:Login>
<br />
<asp:Label ID="l_succ" runat="server" ForeColor="#FF0066"></asp:Label>
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html>
4.login code behind(loginpage.aspx.cs)
***************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string ErrorMessage = "";
string id, FullName = "";
try
{
//SqlConnection con = new SqlConnection("Data Source=servername;Initial Catalog=databasename;User ID=userid;Password=password");
SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=db_user;User ID=sa;Password=nrr");
con.Open();
SqlCommand cmd = new SqlCommand("Sp_LoginCheck", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserName", Login1.UserName);
cmd.Parameters.Add("@Password", Login1.Password);
SqlDataReader read;
read = cmd.ExecuteReader();
read.Read();
id = read.GetValue(0).ToString();
FullName = read.GetValue(1).ToString();
read.Close();
con.Close();
HttpCookie c_id = new HttpCookie("id");
HttpCookie c_fullname = new HttpCookie("FullName");
c_id.Value = id;
c_fullname.Value = FullName;
Response.Cookies.Add(c_id);
Response.Cookies.Add(c_fullname);
if (id != " ")
{
Response.Redirect("Next_Page.aspx");
}
}
catch (Exception eee)
{
//if you want to display error message in UI just like to use l_succ lable.
//l_succ.Text = eee.Message;
ErrorMessage = eee.Message;
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script>alert('"+ErrorMessage+"')</script>");
}
}
}
5.Next_Page.aspx
*****************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Next_Page.aspx.cs" Inherits="Next_Page" %>
<!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></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 635px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<table class="style1" border="30" cellpadding="2" cellspacing="2" >
<tr>
<td class="style2">
<asp:Label ID="L_id" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="l_name" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<br />
<br />
</div>
</form>
</body>
</html>
6.code behind(nextpage.aspx.cs)
******************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Next_Page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
L_id.Text = Request.Cookies["id"].Value.ToString();
l_name.Text = Request.Cookies["FullName"].Value.ToString();
}
}