<%@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="NorthCSharp.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>WebForm1</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <!-- <summary> ||||| Style Sheet ||||| </summary> --><link title="standard" href="Styles.css" type="text/css" rel="stylesheet"> </head> <body> <!-- ||||| Login Form ||||| --> <form id="frmlogin" method="post" runat="server"> <table id="mainTable" border="0"> <tr> <td> <table class="t_border" id="loginTable" cellspacing="15" cellpadding="0"> <tr> <td><b>Login: </b> </td> <td><asp:textbox id="txtUserName" runat="server" width="160px"></asp:textbox><asp:requiredfieldvalidator id="rvUserValidator" runat="server" controltovalidate="txtUserName" errormessage="You must supply a Username!" display="None"></asp:requiredfieldvalidator></td> </tr> <tr> <td><b>Password: </b> </td> <td><asp:textbox id="txtPassword" runat="server" width="160px" textmode="Password"></asp:textbox><asp:requiredfieldvalidator id="rvPasswordValidator" runat="server" controltovalidate="txtPassword" errormessage="Empty Passwords not accepted" display="None"></asp:requiredfieldvalidator></td> </tr> <tr> <td align="center" colspan="2"><asp:button id="cmdSubmit" runat="server" text="Submit" borderstyle="Solid"></asp:button></td> </tr> </table> </td> </tr> <tr> <td> <table id="messageDisplay"> <tr> <td><asp:validationsummary id="Validationsummary1" runat="server" width="472px" displaymode="BulletList"></asp:validationsummary></td> </tr> </table> <!--<asp:hyperlink id="hl_Register" runat="server" navigateurl="Register.aspx" font-size="X-Small" height="8px" width="209px" font-names="MS Reference Sans Serif">New User?...Register Here!</asp:hyperlink>--> </td> </tr> </table> </form> <asp:label id="lblMessage" runat="server" width="288px" font-bold="True" font-italic="True" font-size="Medium" forecolor="#C00000"></asp:label> <asp:label id="lblMessage2" runat="server" width="288px" font-bold="True" font-italic="True" font-size="Medium" forecolor="#C00000"></asp:label> <!-- ||||| End of Form ||||| --> </body> </html>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; // <summmary> // What has been added for Login Page // for this application to function // </summary> using System.Web.Security; using System.Data.SqlClient; using System.Configuration; namespace NorthCSharp { /// <summary> /// Summary description for WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtUserName; protected System.Web.UI.WebControls.RequiredFieldValidator rvUserValidator; protected System.Web.UI.WebControls.TextBox txtPassword; protected System.Web.UI.WebControls.RequiredFieldValidator rvPasswordValidator; 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; 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.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion 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) { SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]); SqlCommand myCmd = new SqlCommand("sp_ValidateUser", myConn); myCmd.CommandType = CommandType.StoredProcedure; 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.Direction = ParameterDirection.Input; objParam2.Direction = ParameterDirection.Input; returnParam.Direction = ParameterDirection.ReturnValue; objParam1.Value = txtUser; objParam2.Value = txtPass; try { if (myConn.State.Equals(ConnectionState.Closed)) { myConn.Open(); myCmd.ExecuteNonQuery(); } if ((int)returnParam.Value < 1) { lblMessage.Text = "Invalid Login!"; return false; } else { myConn.Close(); return true; } } catch (Exception ex) { lblMessage2.Text = ex + "Error Connecting to the database"; return false; } } } }
] <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="strConn" value="Network Library=DBMSSOCN;Data Source=192.168.0.100,1433;database=Northwind;User id=;Password=;"/> </appSettings> <system.web> <!-- DYNAMIC DEBUG COMPILATION ... ... ...
private bool DBConnection(string strUserName, string strPassword) { OleDbConnection MyConn = new OleDbConnection(ConfigurationSettings.AppSettings["strConn"]); OleDbCommand MyCmd = new OleDbCommand("sp_ValidateUser", MyConn); MyCmd.CommandType = CommandType.StoredProcedure; OleDbParameter objParam1, objParam2; objParam1 = MyCmd.Parameters.Add("@UserName", OleDbType.Char); objParam2 = MyCmd.Parameters.Add("@Password", OleDbType.Char); //returnParam = MyCmd.Parameters.Add ("@Num_of_User", OleDbType.Integer); objParam1.Direction = ParameterDirection.Input; objParam2.Direction = ParameterDirection.Input; //returnParam.Direction = ParameterDirection.ReturnValue; objParam1.Value = txtUserName.Text; objParam2.Value = txtPassword.Text; try { if(MyConn.State == ConnectionState.Closed) { MyConn.Open(); } OleDbDataReader objReader; objReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection); while(objReader.Read()) { if ((string)objReader.GetValue(0) != "1") { lblMessage.Text = "Invalid Login!"; //return false; } else { objReader.Close(); return true; } } } catch(Exception ex) { lblMessage2.Text = "Error Connecting to the database!"; //return false; } }
(string)objReader.GetValue(0) != "1"
objReader.GetValue(0) = 0
using System; using System.Collections; using System.ComponentModel; using System.Data; 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; using System.Data.OleDb; using System.Configuration; namespace Login { /// <summary> /// Summary description for WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtUserName; protected System.Web.UI.WebControls.RequiredFieldValidator rvUserValidator; protected System.Web.UI.WebControls.TextBox txtPassword; protected System.Web.UI.WebControls.RequiredFieldValidator rvPasswordValidator; protected System.Web.UI.WebControls.Button cmdSubmit; protected System.Web.UI.WebControls.ValidationSummary Validationsummary1; protected System.Web.UI.WebControls.Label lblMessage2; protected System.Web.UI.WebControls.Label lblMessage; //protected System.Web.UI.Page.Session; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } private bool DBConnection(string strUserName, string strPassword) { string LoginSQL; OleDbConnection MyConn = new OleDbConnection(ConfigurationSettings.AppSettings["strConn"]); OleDbCommand MyCmd = new OleDbCommand("sp_ValidateUser", MyConn); MyCmd.CommandType = CommandType.StoredProcedure; OleDbParameter objParam1, objParam2; objParam1 = MyCmd.Parameters.Add("@UserName", OleDbType.Char); objParam2 = MyCmd.Parameters.Add("@Password", OleDbType.Char); //returnParam = MyCmd.Parameters.Add ("@Num_of_User", OleDbType.Integer); objParam1.Direction = ParameterDirection.Input; objParam2.Direction = ParameterDirection.Input; //returnParam.Direction = ParameterDirection.ReturnValue; objParam1.Value = txtUserName.Text; objParam2.Value = txtPassword.Text; try { if(MyConn.State == ConnectionState.Closed) { MyConn.Open(); } OleDbDataReader objReader; objReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection); while(objReader.Read()) { if (objReader.GetValue(0) = 0) { lblMessage.Text = "Invalid Login!"; //return false; } else { objReader.Close(); return true; } } } catch(Exception ex) { lblMessage2.Text = "Error Connecting to the database!"; //return false; } } #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.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void cmdSubmit_Click(object sender, System.EventArgs e) { if(Page.IsValid) //Meaning the Control Validation was successful! { //Connect to Database for User Validation //int intMaxLoginAttempts = ((Int32)Session["Num_of_Tries"]); if(DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim())) { //if(Session["Logged_IN"].Equals("Yes")) //Use to Validate on other pages in the application //{ //Session["Logged_IN"].Equals("Yes"); FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); //default.aspx Page! //Response.Redirect("default.aspx"); //Response.Redirect("../Login.aspx?ReturnUrl=/default.aspx"); //} //lblMessage.Text = "Success Full"; } else { //Credentials are Invalid lblMessage.Text = "Sorry! Your login or password is incorrect. \n\n Please log in again."; //Session["LoginCount"] = ((Int32)Session["LoginCount"]) + 1; //Response.Redirect ("default.aspx"); } /*if(Session["LoginCount"].Equals(intMaxLoginAttempts)) { Response.Redirect("Denied.aspx"); } if(((Int32)Session["Num_of_Tries"]) > 2) { Response.Redirect("Denied.aspx"); }*/ } } } }
(int)objReader.GetValue(0) = 0
if (objReader.GetValue(0) = 0) { lblMessage.Text = "Invalid Login!"; //return false; }
| DaniWeb Message | |
| Cancel Changes | |