How to Create a simple login in asp.net C#

Posted by Sathiya_Narayanan under ASP.NET on 6/27/2012 | Points: 10 | Views : 52205 | Status : [Member] | Replies : 10
Hi
I m learning c# .net and i want to create a login that can access the username and password fields checks from the database and redirect to particular pages, after they are matched from their respective textboxes. So please tell me how should do this in 3tier application.

SATHIYANARAYANAN


Responses

Posted by: Johnbhatt on: 6/27/2012 [Member] Starter | Points: 25

Up
0
Down
Look at this Thread. Get something from there.

Posting entire Application here is a tough. You should try yourself and in case of Problem or Cofusion, we are here to Help.



John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

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

Posted by: Ajay.Kalol on: 6/27/2012 [Member] Starter | Points: 25

Up
0
Down
http://www.thesmartcodes.com/implementing-3-tier-architecture-in-c-net/

Refer this link

Ajay
ajaypatelfromsanthal.blogspot.in

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

Posted by: Btrack124 on: 6/28/2012 [Member] Starter | Points: 25

Up
0
Down
You can use a simpe login control which you can find in the ToolBox of VisualStudio
In the Login Control Click event
refer the link mentioned below
http://csharpdotnetfreak.blogspot.com/2011/12/login-page-using-login-control-aspnet.html

Thanks
http://www.alacraft.com.au/

Roland Masten

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

Posted by: Johnbhatt on: 6/28/2012 [Member] Starter | Points: 25

Up
0
Down
Our Admin has Craeated an Article for Login Page in ASP.NET driven from Database.

http://www.dotnetfunda.com/articles/article808-how-to-write-a-simple-login-page-in-aspnet.aspx

Have a look and try.


John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

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

Posted by: Premalatha on: 6/28/2012 [Member] Starter | Points: 25

Up
0
Down
It is a Small Console Application,

static void Main(string[] args)
{
string username = null;
string password = null;
Console.WriteLine("Enter Uaername");
username = Console.ReadLine();
Console.WriteLine("Enten Password");
password = Console.ReadLine();
if (username == "username" && password == "password")
{
Console.WriteLine("Welcome " + username);
}
else {
Console.WriteLine("Please Try Again");
}
Console.ReadLine();


}

Premalatha
Software Engineer

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

Posted by: Premalatha on: 6/28/2012 [Member] Starter | Points: 25

Up
0
Down
It is a HTML Code for Design

<form id="form1" runat="server">
<div style="height: 925px; width: 588px; margin-right: 78px">

UserName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TxtName" runat="server" Height="22px" Width="160px"></asp:TextBox>
<br />
<br />
<br />
PassWord&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TxtPwd" runat="server" TextMode="Password" Width="161px"></asp:TextBox>
<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Text="Cancel" />
<br />
<br />
<br />

</div>
</form>

It is C# Code on Login Button.
protected void Button1_Click(object sender, EventArgs e)
{
if (TxtName.Text == "Prema" && TxtPwd.Text == "Latha")
{
Response.Redirect("http://www.google.com");
}
}

Premalatha
Software Engineer

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

Posted by: Ranjeet_8 on: 7/10/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Plz Refer this URL

It will help u to create Login Page.
http://www.c-sharpcorner.com/uploadfile/rohatash/simple-user-login-in-Asp-Net-using-C-Sharp/



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

Posted by: Patel28rajendra on: 7/10/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

refer this link for 3-tier architecture

http://www.dotnetfunda.com/articles/article71.aspx

and
http://www.dotnetfunda.com/articles/article808-how-to-write-a-simple-login-page-in-aspnet.aspx

for Login code

Regards

R D Patel

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

Posted by: Vinay13mar on: 11/14/2012 [Member] Starter | Points: 25

Up
0
Down
please check the below link .


http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=79&title=How To Write a Simple Login Page In Asp.net



Sathiya_Narayanan, 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

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

Login to post response