Monday, March 12, 2012

Manual Authentication from a Database

Assuming that I have a table [users] in a MSSQL 2005 database. the table has two columns. [userId] and [password].

I have an aspx with C# page with two textboxes and a button.

I want to let the user login or send him an error message if the password or the username does not match from the table in database.

I always made this possible through the template database from administrating the website. but never tried to do it as simple as that. I'm lost here!

How can I do it?

well this is full fledges program you are asking us to write, which of course is not the place, but i can tell you steps you can follow.

1. drop two text boxes on form txtUsername and txtUserPassword

2. Make SQL Connection with your database

3. Write a function that connect to your DB and pass the select query with where clause of user name and password (select * from users where userId = ' + txtUserName + ' and password = ' + txtUserPassword + ')

4. If query return records you can ask your function return true indicates valid user or otherwise bad user.

|||

Is there any usefull link that takes these steps in details? any website of your recommendation! anything?

I have to hand on a project in 12/May that uses this kind of authentication!

|||Hi,

Here's the code you can refer:

The Button1_Click Event:

protected void Button1_Click(object sender, EventArgs e) {string username =this.TextBox1.Text;string password =this.TextBox2.Text;// connstr is the name of connection string you set in Web.Configstring conn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; SqlConnection myconn =new SqlConnection(conn); sqltxt ="select * from UserTable where username = '"+username+"' and password = '"+password+"'"; SqlDataAdapter myadpt =new SqlDataAdapter(sqltxt, myconn); DataSet myds =new DataSet(); myadpt.Fill(myds);if (myds.Tables[0].Rows.Count > 0) {// login successfully }else {// error message } }

aspx file:

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
Thanks.

No comments:

Post a Comment