mathieu_cupryk
asked on
Login Easy URGENT.
I have a login Page. When I click on submit it never validates. There must be something missing.
aspx code:
<%@ Register TagPrefix="mspos" TagName="header" Src="~/Engine/Controls/hea der.ascx" %>
<%@ Register TagPrefix="uc1" TagName="footer" Src="Engine/Controls/foote r.ascx" %>
<%@ Page language="c#" Codebehind="login.aspx.cs" AutoEventWireup="false" Inherits="MSPOS.login" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii ">
<meta content="C#" name="CODE_LANGUAGE">
<meta http-equiv="Page-Exit" content="blendTrans(Durati on=1.0)">
</HEAD>
<body background="images/bg.gif" leftMargin="0" topMargin="0" marginheigh="0" marginwidth="0"
bgColor="#fef9e9">
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr>
<td>
<mspos:header id="Header1" runat="server"></mspos:hea der>
</td>
<LINK rel="stylesheet" type="text/css" href="http://localhost/MSPOS/mspos.css">
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td valign="middle" align="center">
<form id="login" name="form1" method="post" runat="server">
<asp:validationsummary id="ValidationSummary1" runat="server" HeaderText="The following error(s) occurred while login in:"></asp:validationsumma ry>
<P>
<asp:label id="lbPrompt" runat="server"></asp:label ></P>
<BR>
<table bordercolor="#cccc99" cellspacing="0" cellpadding="0" width="369" align="center"
border="2" bgColor="#fef9e9" height="176">
<tbody>
<tr>
<td align="center">
<table cellspacing="4" cellpadding="3" width="100%" background="file:///D|/web projects/v ipdotnet/i mages/tdba ck.jpg"
border="0">
<tbody>
<tr>
<td nowrap colspan="2">
<P align="center"><FONT face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="2"><STRONG>Please Enter your Username and Password</STRONG></FONT></ P>
</td>
</tr>
<tr>
<td>
<P align="right">
<IMG height="40" src="images/key.gif" width="36"> </P>
</td>
<td> <FONT color="#adae7d"><STRONG><F ONT face="Arial">User Login</FONT></STRONG></FON T>
</td>
</tr>
<tr>
<td>
<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">User Name:</font>
</div>
</td>
<td>
<asp:textbox id="UserName" runat="server" TextMode="SingleLine" MaxLength="32" Width="150px"></asp:textbo x>
<asp:requiredfieldvalidato r id="RequiredFieldValidator 1" runat="server" ControlToValidate="Usernam e" Display="Dynamic"
ErrorMessage="You must enter a Username">*</asp:requiredf ieldvalida tor>
</td>
</tr>
<tr>
<td>
<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Password:</font>
</div>
</td>
<td>
<asp:textbox id="Password" runat="server" TextMode="Password" MaxLength="40" Width="150px"></asp:textbo x>
<asp:requiredfieldvalidato r id="RequiredFieldValidator 2" runat="server" ControlToValidate="Passwor d" Display="Dynamic"
ErrorMessage="You must enter a Password">*</asp:requiredf ieldvalida tor>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button id="Button" runat="server" Text="Submit"></asp:Button ></td>
</tr>
</tbody>
</table>
<asp:label id="ErrorMsg" runat="server" ForeColor="Red"></asp:labe l>
</td>
</tr>
</tbody>
</table>
</form>
<uc1:footer id="Footer1" runat="server"></uc1:foote r>
<asp:Label id="Message" runat="server"></asp:Label ><BR>
</td>
</tr>
</tbody>
</table>
</body>
</HTML>
aspx.cs code:
------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data.Odbc;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
using System.Security.Cryptograp hy;
using System.Security.Principal;
using System.Security.Permission s;
using System.EnterpriseServices;
using System.Web.Security;
namespace MSPOS
{
/// <summary>
/// Summary description for login.
/// </summary>
///
public class login : System.Web.UI.Page
{
protected System.Web.UI.WebControls. TextBox UserName;
protected System.Web.UI.WebControls. TextBox Password;
protected System.Web.UI.WebControls. Label Message;
protected System.Web.UI.WebControls. Button Button;
protected System.Web.UI.WebControls. Validation Summary ValidationSummary1;
protected System.Web.UI.WebControls. Label lbPrompt;
protected System.Web.UI.WebControls. RequiredFi eldValidat or RequiredFieldValidator1;
protected System.Web.UI.WebControls. RequiredFi eldValidat or RequiredFieldValidator2;
protected System.Web.UI.WebControls. Label ErrorMsg;
protected System.Web.UI.HtmlControls .HtmlForm form1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#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.UserName.TextChanged += new System.EventHandler(this.U serName_Te xtChanged) ;
this.Password.TextChanged += new System.EventHandler(this.P assword_Te xtChanged) ;
this.Button.Click += new System.EventHandler(this.B utton_Clic k);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
//Redirect page after login succeed
public string RedirectPage ="AdminStart.aspx";
public int chk;
bool Authenticate(string username, string password)
{
bool authenticated = false;
try
{
string Stringdsn="DSN=MSPOS;UID=s ystem;PWD= system;";
string AccessLevel = null;
string strSQL = "select * from TBUSERS where USERNAME='" + username + "' and PASSWD='" + password + "'";
OdbcConnection Conn = new OdbcConnection(Stringdsn);
OdbcCommand Cmd = new OdbcCommand(strSQL, Conn);
OdbcDataReader myRead=null; //instancing a datareader
Conn.Open(); //opening the connection
myRead = Cmd.ExecuteReader();
if (myRead!=null)
{ //checking for records
if (myRead.Read())
{
if (myRead.GetString(1) == username)
{
//checking username in db with given username
if(myRead.GetString(2) == password)
{ //checking password in db with given password
AccessLevel = myRead.GetString(5);
Session["Authenticated"] = AccessLevel;
if (AccessLevel == "1") // Admin role
Session.Timeout = 5;
if (AccessLevel == "2") // CC-Manager
Session.Timeout = 15;
if (AccessLevel == "3") // C-Cashier
Session.Timeout = 15;
if (AccessLevel == "4") // Cashier
Session.Timeout = 15;
authenticated =true;
}
else
{
chk=1; // settingflag as 1 if password failed
}
}
else
{
chk=2; //setting flaga as 2 in username failed
}
}
}
}
catch(Exception e)
{
Response.Write("Auth Exception: " + e.ToString());
}
return authenticated;
}
private void Button_Click(Object sender, EventArgs e)
{
if (Authenticate(UserName.Tex t, Password.Text))
{
string AccessLevel;
AccessLevel = Session["Authenticated"].T oString();
if (AccessLevel == "1")
Response.Redirect("AdminSt art.aspx") ;
else
if (AccessLevel == "2")
Response.Redirect("CCManSt art.aspx") ;
else
if (AccessLevel == "3")
Response.Redirect("CCashie rStart.asp x");
else
if (AccessLevel == "4")
Response.Redirect("Cashier Start.aspx ");
else
Response.Redirect ("Error.aspx");
}
else
{
if (chk==1)
{
Message.Text= "Invalid PassWord";
Message.Visible = true;
}
else
{
Message.Text= "Invalid UserName";
Message.Visible = true;
}
}
}
private void UserName_TextChanged(objec t sender, System.EventArgs e)
{
}
private void Password_TextChanged(objec t sender, System.EventArgs e)
{
}
}
}
aspx code:
<%@ Register TagPrefix="mspos" TagName="header" Src="~/Engine/Controls/hea
<%@ Register TagPrefix="uc1" TagName="footer" Src="Engine/Controls/foote
<%@ Page language="c#" Codebehind="login.aspx.cs"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii ">
<meta content="C#" name="CODE_LANGUAGE">
<meta http-equiv="Page-Exit" content="blendTrans(Durati
</HEAD>
<body background="images/bg.gif"
bgColor="#fef9e9">
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr>
<td>
<mspos:header id="Header1" runat="server"></mspos:hea
</td>
<LINK rel="stylesheet" type="text/css" href="http://localhost/MSPOS/mspos.css">
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td valign="middle" align="center">
<form id="login" name="form1" method="post" runat="server">
<asp:validationsummary id="ValidationSummary1" runat="server" HeaderText="The following error(s) occurred while login in:"></asp:validationsumma
<P>
<asp:label id="lbPrompt" runat="server"></asp:label
<BR>
<table bordercolor="#cccc99" cellspacing="0" cellpadding="0" width="369" align="center"
border="2" bgColor="#fef9e9" height="176">
<tbody>
<tr>
<td align="center">
<table cellspacing="4" cellpadding="3" width="100%" background="file:///D|/web
border="0">
<tbody>
<tr>
<td nowrap colspan="2">
<P align="center"><FONT face="Verdana, Arial, Helvetica, sans-serif" color="#000000" size="2"><STRONG>Please Enter your Username and Password</STRONG></FONT></
</td>
</tr>
<tr>
<td>
<P align="right">
<IMG height="40" src="images/key.gif" width="36"> </P>
</td>
<td> <FONT color="#adae7d"><STRONG><F
</td>
</tr>
<tr>
<td>
<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">User Name:</font>
</div>
</td>
<td>
<asp:textbox id="UserName" runat="server" TextMode="SingleLine" MaxLength="32" Width="150px"></asp:textbo
<asp:requiredfieldvalidato
ErrorMessage="You must enter a Username">*</asp:requiredf
</td>
</tr>
<tr>
<td>
<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Password:</font>
</div>
</td>
<td>
<asp:textbox id="Password" runat="server" TextMode="Password" MaxLength="40" Width="150px"></asp:textbo
<asp:requiredfieldvalidato
ErrorMessage="You must enter a Password">*</asp:requiredf
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button id="Button" runat="server" Text="Submit"></asp:Button
</tr>
</tbody>
</table>
<asp:label id="ErrorMsg" runat="server" ForeColor="Red"></asp:labe
</td>
</tr>
</tbody>
</table>
</form>
<uc1:footer id="Footer1" runat="server"></uc1:foote
<asp:Label id="Message" runat="server"></asp:Label
</td>
</tr>
</tbody>
</table>
</body>
</HTML>
aspx.cs code:
------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data.Odbc;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using System.Security.Cryptograp
using System.Security.Principal;
using System.Security.Permission
using System.EnterpriseServices;
using System.Web.Security;
namespace MSPOS
{
/// <summary>
/// Summary description for login.
/// </summary>
///
public class login : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.HtmlControls
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#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.UserName.TextChanged += new System.EventHandler(this.U
this.Password.TextChanged += new System.EventHandler(this.P
this.Button.Click += new System.EventHandler(this.B
this.Load += new System.EventHandler(this.P
}
#endregion
//Redirect page after login succeed
public string RedirectPage ="AdminStart.aspx";
public int chk;
bool Authenticate(string username, string password)
{
bool authenticated = false;
try
{
string Stringdsn="DSN=MSPOS;UID=s
string AccessLevel = null;
string strSQL = "select * from TBUSERS where USERNAME='" + username + "' and PASSWD='" + password + "'";
OdbcConnection Conn = new OdbcConnection(Stringdsn);
OdbcCommand Cmd = new OdbcCommand(strSQL, Conn);
OdbcDataReader myRead=null; //instancing a datareader
Conn.Open(); //opening the connection
myRead = Cmd.ExecuteReader();
if (myRead!=null)
{ //checking for records
if (myRead.Read())
{
if (myRead.GetString(1) == username)
{
//checking username in db with given username
if(myRead.GetString(2) == password)
{ //checking password in db with given password
AccessLevel = myRead.GetString(5);
Session["Authenticated"] = AccessLevel;
if (AccessLevel == "1") // Admin role
Session.Timeout = 5;
if (AccessLevel == "2") // CC-Manager
Session.Timeout = 15;
if (AccessLevel == "3") // C-Cashier
Session.Timeout = 15;
if (AccessLevel == "4") // Cashier
Session.Timeout = 15;
authenticated =true;
}
else
{
chk=1; // settingflag as 1 if password failed
}
}
else
{
chk=2; //setting flaga as 2 in username failed
}
}
}
}
catch(Exception e)
{
Response.Write("Auth Exception: " + e.ToString());
}
return authenticated;
}
private void Button_Click(Object sender, EventArgs e)
{
if (Authenticate(UserName.Tex
{
string AccessLevel;
AccessLevel = Session["Authenticated"].T
if (AccessLevel == "1")
Response.Redirect("AdminSt
else
if (AccessLevel == "2")
Response.Redirect("CCManSt
else
if (AccessLevel == "3")
Response.Redirect("CCashie
else
if (AccessLevel == "4")
Response.Redirect("Cashier
else
Response.Redirect ("Error.aspx");
}
else
{
if (chk==1)
{
Message.Text= "Invalid PassWord";
Message.Visible = true;
}
else
{
Message.Text= "Invalid UserName";
Message.Visible = true;
}
}
}
private void UserName_TextChanged(objec
{
}
private void Password_TextChanged(objec
{
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.