| | |
Simple ASP.Net Login Page using C#
![]() |
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
hi paladine, i'm new to .net and was working on this login page,thanks a lot for ur tutorial...
i'm using MS access data base,database name is bank,i dont have any user id and password set.
</system.web>
<appSettings>
<addkey="ConnectionString"value="User Id=;Password=;Data Source=bank;"/>
</appSettings>
in the code behind file
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
namespace login
{
///<summary>
/// Summary description for WebForm1.
///</summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtUserName;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.RequiredFieldValidator rvUserValidator;
protected System.Web.UI.WebControls.Button cmdSubmit;
protected System.Web.UI.WebControls.ValidationSummary Validationsummary1;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.Label lblMessage2;
protected System.Web.UI.WebControls.RequiredFieldValidator rvPasswordValidator;
public System.Data.OleDb.OleDbConnection conn;
public System.Data.OleDb.OleDbCommand dataCmd;
public System.Data.OleDb.OleDbDataReader objReader;
public System.Data.OleDb.OleDbDataReader dataRead;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
conn=new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\vamshi\\test\\bank.mdb");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///</summary>
private void InitializeComponent()
{
this.txtUserName.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void cmdSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
{
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, false);
}
else
{
lblMessage.Text = "Invalid Login, please try again!";
}
}
}
private bool DBConnection(string txtUser, string txtPass)
{
if ( ConfigurationSettings.AppSettings ["ConnectionString"]== "") throw new Exception ("Connection String not found");
else
{
//SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
//SqlCommand myCmd = new SqlCommand("sp_ValidateUser", myConn);
conn=new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\vamshi\\test\\bank.mdb");
conn.Open();
dataCmd = new OleDbCommand("Query1" ,conn);
dataCmd.CommandType = CommandType.StoredProcedure;
OleDbParameter objParam1;
OleDbParameter objParam2;
//OleDbParameter returnParam;
//OleDbCommand comm = new OleDb.OleDbCommand(sql, conn);
objParam1=dataCmd.Parameters.Add("@userid",OleDbType.VarChar);
objParam2=dataCmd.Parameters.Add("@password",OleDbType.VarChar);
//returnParam=dataCmd.Parameters.Add("@Num_of_User",OleDbType.Integer);
objParam1.Direction = ParameterDirection.Input;
objParam2.Direction = ParameterDirection.Input;
//returnParam.Direction = ParameterDirection.ReturnValue;
//OleDbDataReader reader = dataCmd.ExecuteReader();
//SqlParameter objParam1;
//SqlParameter objParam2;
//SqlParameter returnParam;
//objParam1 = myCmd.Parameters.Add ("@UserName", SqlDbType.VarChar);
//objParam2 = myCmd.Parameters.Add ("@Password", SqlDbType.VarChar);
//returnParam = myCmd.Parameters.Add ("@Num_of_User", SqlDbType.Int);
objParam1.Value = txtUserName.Text;
objParam2.Value = txtPassword.Text;
try
{
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
dataCmd.ExecuteNonQuery();
}
//if ((int)returnParam.Value < 1)
objReader = dataCmd.ExecuteReader(CommandBehavior.CloseConnection);
while(objReader.Read())
{
if((bool)objReader.GetValue(0) == false)
{
lblMessage.Text = "Invalid Login!";
//return false;
}
else
{
conn.Close();
return true;
}
}
}
catch (Exception ex)
{
lblMessage2.Text = ex + "Error Connecting to the database";
}
}
return false;
}
}
}
when i compile i get an error connecting to the database...
can u plz help me with this..
thank u in advance
HI Vamshi
The VS 2005 have a readymade tool in its toolbox called as “Login”.
You just have to Drag & Drop that on your .aspx page
After drag & drop you can change that to your desired design by clicking on Auto Format
Then we need to go to the ‘Properties’ of that Login Control and click on Events.
There we can see a event called Authenticate, Double click on it.
Once that is done. You have to write the C# code in that event
This is the code its work fine try out
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
try
{
string uname = Login1.UserName.Trim();//Get User Name From the control
string password = Login1.Password.Trim();//Get Password From The Control
bool flag=AuthenticateUser(uname,password);
if (flag == true)
{
e.Authenticated = true;
Login1.DestinationPageUrl = "Employee.aspx";
}
else
{e.Authenticated = false;}
}
catch(Exception)
{e.Authenticated=false; }
}
public bool AuthenticateUser(string uname, string password)
{
bool bflag = false;
string connectionString = WebConfigurationManager.ConnectionStrings["TraveLineConnectionString"].ConnectionString;
string sql = "select * from Employees where EmpID ='" + uname + "' AND Password ='" + password + "'";
DataSet EmpDS = new DataSet();
SqlConnection con;
SqlDataAdapter da;
try
{
con = new SqlConnection(connectionString);
con.Open();
da = new SqlDataAdapter(sql, con);
da.Fill(EmpDS);
con.Close();
}
catch (Exception)
{
EmpDS = null;
}
if (EmpDS != null)
{
if(EmpDS.Tables[0].Rows.Count > 0)
bflag = true;
}
return bflag;
}
hope this is will help u
Its So Simple isn’t it!
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 1
i want coding for registeration process in asp.net using c# i want it in c# not html.so please will u able to reply me or u can send it to my mail id rajamani.bonton@gmail.com
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 187
The previous poster DID give you code for aspx, Id suggest you quit moaning people arent doing all your work for you, and try doing it yourself.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
When I try to do a simple logon based on this code... I get this error..
"Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."
When i disable enableViewStateMac = false it works and I dont get the error. What am I missing ?
Thanks MUCH!!!
"Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."
When i disable enableViewStateMac = false it works and I dont get the error. What am I missing ?
Thanks MUCH!!!
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: how to convert words to numerals
- Next Thread: Login Form Help
Views: 181126 | Replies: 69
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net audio bitmap box button c# call check checkbox class code color control conversion csharp custom data database datagrid datagridview dataset datetime datetimepicker degrees deployment display dll drawing event excel file files form format forms function gcd gdi+ graphics grid http image index input instance internet key list listbox login loop math multithreading mysql networking notepad operator oracle picturebox prime print problem procedure programming property reflection remote remoting resource save saving search sendkeys server socket sql statistics stream string tcp text textbox threading time timer update validation vc++ visual webbrowser windows winforms wpf xml






