Resources » Code Snippets » ASP.NET WebForms

code for login using C#


Posted Date: 20-Mar-2009  Last Updated:   Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 10



Code for login validation:
create table:
Table Name: login details
field: login, Datatype: Text
password, DataType: Text.

Code is design in such a way that every user are not allow to visit evert page.
suppose if u r not a administrator then u will not able to see the page newentry.aspx, u will redirect on default.aspx.



protected void logins(string username, string password)
{
string s;
OleDbConnection cn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;" + @"Data Source= D:\rajni\App_Data\rajni.mdb");
OleDbCommand cmd = new OleDbCommand("SELECT password from logindetails where login = @id;", cn);
OleDbParameter par = cmd.Parameters.Add("@id", OleDbType.VarChar);
par.Value = username;
DataSet ds = new DataSet("details");
OleDbDataAdapter details = new OleDbDataAdapter();
details.SelectCommand = cmd;
details.Fill(ds);
string dbpassword = null;
string givenpassword = TextBox2.Text;
try
{
DataRow dr = ds.Tables[0].Rows[0];
cn.Open();
dbpassword = (string)dr["password"];
if (dbpassword == givenpassword)
{
Session["username"] = TextBox1.Text;
s = Convert.ToString(Session["username"]);
Session["admin"] = s;
if (s == "administrator")
{
Response.Redirect("newentry.aspx");
}
else
{
Response.Redirect("Default.aspx");
}
}
else
{
Label1.Text = "Invalid password";
}
}
catch
{
cn.Close();
Label1.Text = "Invalid login and password";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox1.Focus();
}


}


Did you like this resource? Share it with your friends and show your love!

Responses to "code for login using C#"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India