Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > BOOK: Beginning ASP.NET 1.0
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 1.0
This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 28th, 2004, 09:59 PM
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to waru
Default form-based authentication using a database

I want to check if the user name and password the user provides to textboxes is the same with those in a database and if so show him his details but i can't make it work. Here is the code:
---------------------------------------------------------------------
THE CS FILE

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.OleDb;

public class myUserControl2: UserControl{
    public TextBox txtUID;
    public TextBox txtPWD;
    public Label lbl;

  public void myClick(object sender, EventArgs e){
      string strCon, strSQL;
      strCon = "Provider=Microsoft.Jet.OLEDB.4.0;";
      strCon += "Data Source=" + Server.MapPath("userInfo.mdb");
      strSQL = "SELECT userID, userName, userPassword,userInfo FROM usersTable";
try{
   OleDbConnection objCon = new OleDbConnection(strCon);
   objCon.Open();
   OleDbCommand objCmd = new OleDbCommand(strSQL,objCon);
    OleDbDataReader objReader = objCmd.ExecuteReader (CommandBehavior.CloseConnection);
while(objReader.Read()){
   if(objReader["userName"].ToString()== txtUID.Text && objReader ["userPassword"].ToString()==txtPWD.Text){
lbl.Text = "success!" + "<br>" + objReader["userPassword"].ToString();
}
else{
  lbl.Text = "failure" + "<br>" + objReader["userName"] + "<br>" +
   objReader["userPassword"].ToString() +
  "<br>" + objReader["userInfo"].ToString();
}
}
objReader.Close();
objCon.Close();
}
catch(OleDbException objError){
if(objError.Message.Substring(0,19)=="Could not find file"){
lbl.Text = objError.Message + objError.Source;
}
else{
   lbl.Text = objError.Message + objError.Source;
}
}

}
}
---------------------------------------------------------------------THE ASCX FILE

<%@ Control Inherits="myUserControl2" Src="ch15_5_UserControl.cs"%>

<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
  User name:<asp:TextBox id="txtUID" runat="server"/><br/>
  Password:<asp:TextBox id="txtPWD" runat="server" TextMode="password"/></br>
<asp:button id="btn" runat="server" onClick="myClick" Text="Submit"/><br/>
<asp:RequiredFieldValidator id="rfv" Runat="server" ControlToValidate="txtUID" ErrorMessage="Enter user name"/>
<asp:RequiredFieldValidator id="rfv2" Runat="server" ControlToValidate="txtPWD" ErrorMessage="Enter password"/>
</form>
<asp:label id="lbl" runat="server"/>
</body>
</html>
---------------------------------------------------------------------THE ASPX FILE

<%@ Page Language="C#" Debug="true"%>
<%@ Register TagPrefix="myUC" TagName="myUserControl2" Src="ch15_5_UserControl.ascx"%>
<html>
<head>
<title></title>
</head>
<body>
    <myUC:myUserControl2 id="myU" runat="server"/>


</body>
</html>


  #2 (permalink)  
Old May 28th, 2004, 10:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

Just to verify, you are setting up the click event; I didn't see an event handler declaration; did I miss something? Also, if you think your problem is on the database side, what you can do is change your query to:

strSQL = "SELECT userID, userName, userPassword,userInfo FROM usersTable where userID = '" + txtUID.Text + '" and userPassword = '" + txtPWD.Text + "'";

If there are rows present, then the account credentials are correct.

Brian
  #3 (permalink)  
Old May 28th, 2004, 10:37 PM
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to waru
Default

Hello. Thanks for the help but it still doesn't work. About the onClick event i'm setting it:

public void myClick(object sender, EventArgs e){
  if(txtUID.Text=="waru@hotmail.com" && txtPWD.Text=="treyxxxx"){
    lbl.Text = "Welcome waru";
  }
  else if((txtUID.Text=="akis@hotmail.com" && txtPWD.Text=="vgsxxxxx") ||
(txtUID.Text=="nikiforos@hotmail.com" && txtPWD.Text=="nikifxxx")){
lbl.Text = "Welcome";
}
else{
  lbl.Text="Wrong user name or password";

}
}
I'm sure there is no problem with the database. I noticed(using a label) that the DataReader keeps one record(one user name, password etc) whitch doesn't change when i click the button. Another strange thing is that once in a while it works!



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Form based Authentication shujamughal ASP.NET 2.0 Professional 3 May 25th, 2007 05:34 AM
Form-based Authentication Error lionstudio JSP Basics 2 August 4th, 2006 09:05 AM
Form based authentication.... avanishp General .NET 2 June 17th, 2005 03:11 AM
Form based authentication with WLS7 jcgarciap J2EE 1 March 9th, 2005 01:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.