Click here to Skip to main content
15,559,275 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using Visual studio 2013 and I'm developing an ASP.NET Web application. I dragged and dropped login control from tool box. Its form name is 'Form1', Login ID is 'log1'. I tried to access the text box in the login form named 'UserName' and 'Password' with 'log1.UserName' and 'log1.Password' . I always get this error, 'the name 'log1' doesn't exist in the current context. What should I do? Can anyone help? Thanks in Advance!

My code behind file home.aspx.cs:
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        }
    }
    static int count = 0;
protected void log1_Authenticate(object sender, AuthenticateEventArgs e)
{
    if (log1.UserName == "Admin" && log1.Password == "Admin")
    {
        Response.Redirect("Adminhome.aspx");
    }
    else if (YourValidationFunction(log1.UserName, log1.Password))
    {
        Session["User"]=log1.UserName;
        e.Authenticated = true;
        Response.Redirect("userhome.aspx");
        log1.TitleText = "Successfully Logged In";
    }
    else
    {
        e.Authenticated = false;
        count++;
        if (count >= 3)
        {
            count = 0;
            Session["User"] = log1.UserName;
            Server.Transfer("MainPage.aspx");
        }
    }
}
SqlConnection strConnection = new SqlConnection("server=.\\SQLEXPRESS;database=honeypot;integrated     security=true;");
    private bool YourValidationFunction(string UserName, string Password)
    {
        bool boolReturnValue = false;
        String SQLQuery = "SELECT UserName, Password FROM Register";
        SqlCommand command = new SqlCommand(SQLQuery, strConnection);
        SqlDataReader Dr;
        strConnection.Open();
        Dr = command.ExecuteReader();
        while (Dr.Read())
        {
            if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))
            {
                boolReturnValue = true;
            }
        }
        Dr.Close();
        return boolReturnValue;
    }
    protected void lnkRegis_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdUserAcc.aspx");
    }
}


Home.aspx:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="CodeInjection4.Home" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
        </div>
            <asp:Login ID="log1" runat="server">
                <layouttemplate>
                    <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
                        <tr>
                            <td>
                                <table cellpadding="0">
                                    <tr>
                                        <td align="center" colspan="2">Log In</td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="UserName" runat="server">
                                            <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="log1">*
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="Password" runat="server" TextMode="Password">
                                            <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="log1">*
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="center" colspan="2" style="color:Red;">
                                            <asp:Literal ID="FailureText" runat="server" EnableViewState="False">
                                        </td>
                                     </tr>
                                    <tr>
                                        <td align="right" colspan="2">
                                            <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="log1" />
                                        </td>
                                     </tr>
                               </table>
                            </td>
                        </tr>
                    </table>
                </layouttemplate>
Posted
Updated 13-Jul-15 4:30am
v2
Comments
Richard Deeming 13-Jul-15 9:43am    
Don't store passwords in plain text. Store a salted hash of the password instead:

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Our password hashing has no clothes - Troy Hunt[^]

And you probably want to get rid of the "back door" user as well, or at least give it a sensible password. Admin + Admin is too easy to guess!
Member 11833728 13-Jul-15 10:39am    
Thank you. I will change it. This is just a test page.
F-ES Sitecore 13-Jul-15 10:33am    
Check the designer file (should be something like yourform.designer.cs) and make sure the log1 property has been created for you, sometimes it isn't. If not, look at how the other control properties in that file are defined and manually add one for your login control.
Member 11833728 13-Jul-15 10:37am    
Protected global::System.Web.UI.HtmlControls.Login log1;

Its there
F-ES Sitecore 13-Jul-15 10:53am    
Is the "Inherits" attribute on your aspx page correct? It is inheriting CodeInjection4.Home yet your code-behind doesn't seem to be putting the Home class in the CodeInjection4 namespace.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900