Best way to create login page

Posted by Krishnamanohar under ASP.NET on 1/24/2013 | Points: 10 | Views : 1276 | Status : [Member] | Replies : 2
Hi Members,
i am planning to create a new website ,for that one login page is needed
in that my doubt is for that which one is the best way either 1)login control or 2)Placing the controls (username(label)--textbox1,<br>Password(label):Textbox2<br>submitt(button)
if first one is the best way then why?
if second one is the best way then why?
or any other approach is there?

For Live Projects which one is best one

--------------------------------------------------------------
Thanks in Advance
Krish




Responses

Posted by: Ankitsrist on: 1/24/2013 [Member] Starter | Points: 25

Up
0
Down
if u are making login page so u need to check whether user has an account, u can achieve this by simple aspx design and simple stored procedure following is the complete code.....
In .aspx copy paste the following
<body>

<form id="form1" runat="server">
<div>
<br />
<table>
<tr>
<td style="width: 148px">
name</td>
<td style="width: 150px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 148px">
password</td>
<td style="width: 150px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 148px; height: 6px;">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></td>
<td style="width: 150px; height: 6px;">
<asp:Label ID="Label1" runat="server"></asp:Label><p class="MsoNormal" style="margin: 0in 0in 10pt">
<span style="font-size: 11pt; font-family: 'Arial Unicode MS','sans-serif'; mso-fareast-font-family: 'Times New Roman';
mso-fareast-theme-font: minor-fareast"></span>
</p>
</td>
</tr>
</table>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
</div>
</form>
</body>

-------------------------------
following is code for .aspx.cs, as i have used separate class for connection strings u can change accordng
  

using System.Drawing;
protected void Button1_Click(object sender, EventArgs e)
{
dbclass.mycon();
SqlCommand cmd = new SqlCommand("memberlogin", dbclass .con);
cmd.CommandType =CommandType.StoredProcedure;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@password", SqlDbType.Int).Value = TextBox2.Text;
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
Label1.ForeColor = Color.Green;
Label1.Text="login successfuly,user found";

}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "user not found";
}
}

--------------
then make the table in sqlserver
CREATE TABLE [dbo].[stuloginstoreproc]

(
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[password] [int] NULL,
)

----------------------
following is the simple stored procedure in programmability section of sql server database
create PROCEDURE [dbo].[memberlogin]

@name varchar(50),
@password int
AS
BEGIN

SET NOCOUNT ON;

SELECT * from stuloginstoreproc where name=@name and password=@password
END


its done, if u need further help u can ask me
mark if this helps u
ankit saxena

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Stored Procedure

CREATE procedure login_pro
(
@UserName varchar(50),
@Password varchar(50)
)
as
declare @status int
if exists(select * from Login where UserName=@UserName and Password=@Password)
set @status=1
else
set @status=0
select @status


Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Login_Page_Using_Stored_Procedure._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>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Label ID="Label1" runat="server" Text="Name" Font-Bold="True"
Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox_user_name" ErrorMessage="Please enter username"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label2" runat="server" Text="Password" Font-Bold="True"
Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600"
TextMode="Password" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox_password" ErrorMessage="Please enter password"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True"
BackColor="#CCFF99" onclick="btn_login_Click" /><br />
<asp:Label ID="lblmessage" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />
</div>
</form>
</body>
</html>

Default.aspx.cs

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_Stored_Procedure
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlParameter UserName, Password;

protected void btn_login_Click(object sender, EventArgs e)
{
UserName = new SqlParameter();
Password = new SqlParameter();
SqlConnection con = new SqlConnection(strConnString);

com=new SqlCommand();
com.Connection = con;
con.Open();
Session["UserName"] = TextBox_user_name.Text;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "login_pro";

UserName.SqlDbType = SqlDbType.VarChar;
UserName.Size = 50;
UserName.ParameterName = "@UserName";
UserName.Value = TextBox_user_name.Text.ToString();
UserName.Direction = ParameterDirection.Input;

Password.SqlDbType = SqlDbType.VarChar;
Password.Size = 50;
Password.ParameterName = "@Password";
Password.Value = TextBox_password.Text.ToString();
Password.Direction = ParameterDirection.Input;

com.Parameters.Add(UserName);
com.Parameters.Add(Password);

int status;
status = Convert.ToInt16(com.ExecuteScalar());

if (status == 1)
{
Response.Redirect("Welcome.aspx");
}
else
{
lblmessage.Text = "Invalid UserName and Password...";
}
con.Close();
}
}
}


Welcome.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="Login_Page_Using_Stored_Procedure.Welcome" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />
</div>
</form>
</body>
</html>

Welcome.aspx.cs


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;

namespace Login_Page_Using_Stored_Procedure
{
public partial class Welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lb1.Text = "WELLCOME :: " + Session["UserName"];
}
}
}


If this post helps you mark it as answer
Thanks

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response