2

I have a function that is not being called. I tried to use debugging to write a file, it correctly writes the first debug message "debug0". But not the other messages.

Here is aspx:

<%@PageLanguage="C#"AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC >

<html>
    <head runat="server">
        <title/>
        <link rel="stylesheet" type="text/css" href=".//style.css" />
    </head>
    <body>
        <form class="container" runat="server">
            <asp:login id="Login1" runat="server" style="width: 100%;">
                <LayoutTemplate>
                    <div id="content">
                        <h1>blah<br>Site Login </h1>
                        <asp:TextBox class="field" placeholder="Username" ID="username" runat="server"> </asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*
                        </asp:RequiredFieldValidator>
                        <br>
                        <asp:TextBox class="field" placeholder="Password" ID="password" runat="server" TextMode="Password"/>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*
                        </asp:RequiredFieldValidator>
                        <br>
                        <asp:Button class="btn" ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"/>
                        <br>
                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"/>
                    </div>
                </LayoutTemplate>
            </asp:login>
        </form>
    </body>
</html>

And code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug0");
    }

    protected void Login1_Authenticate(object sender,AuthenticateEventArgs e)
    {
        File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug1");
        SqlConnection con = new SqlConnection();
        try
        {       
            con.ConnectionString =ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
            con.Open();

            string command = "SELECT Username FROM Members WHERE Usernam = @Username AND Password = @Password";

            SqlCommand comm = new SqlCommand(command, con);
            comm.Parameters.Add(new SqlParameter("Username", Login1.UserName));
            comm.Parameters.Add(new SqlParameter("Password", Login1.Password));

            SqlDataReader reader = comm.ExecuteReader();
            if (reader.Read())
            {
                File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug2");

                if (reader["Username"].ToString() == "1")
                {
                    Response.Redirect("Upload/index.php", false);
                }
            }
        }
        catch (Exception ex)
        {
            File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug3");
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            if (con != null)
                con.Close();
        }
    }
}
kaveman
  • 4,339
  • 25
  • 44
Echo
  • 23
  • 5
  • Backticks are only for inline code, if you have a large block of code you want to display you must indent it with 4 spaces or highlight the code then hit the `{ }` on the tool bar of the text editor of the website. – Scott Chamberlain Jun 24 '15 at 21:07
  • this function is no where called on your aspx file : Login1_Authenticate – Nitin Aggarwal Jun 24 '15 at 21:12

1 Answers1

5

It seems that you miss to bind the event in your asp.net markup

   <asp:login 
       id="Login1" 
       runat="server" 
       style="width: 100%;"
       OnAuthenticate="Login1_Authenticate">
Steve
  • 213,761
  • 22
  • 232
  • 286