ASP.net The name 'userName' does not exist in the current context - c#

I've clicked over 20 links trying to solve this problem in different websites.
Please help me
So I am trying to do a simple log in authentication.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void loginButton_Click(object sender, EventArgs e)
{
if(FormsAuthentication.Authenticate(UserName.Text, Password.Text)
{
}
}
}
But an error message pops up saying "The name 'userName' does not exist in the current context' and The name 'Password' does not exist in the current context.
Here is the aspx code
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Account_Login" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
<hgroup class="title">
<h1>Login Page</h1>
</hgroup>
<section id="loginForm">
<asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
<LayoutTemplate>
<p class="validation-summary-errors">
<asp:Literal runat="server" ID="FailureText" />
</p>
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
<asp:Label ID="Label1" runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
</li>
<li>
<asp:Label ID="Label2" runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
</li>
<li>
<asp:CheckBox runat="server" ID="RememberMe" />
<asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
</li>
</ol>
<asp:Button ID="loginButton" runat="server" CommandName="Login" Text="Log in" OnClick="loginButton_Click" />
</fieldset>
</LayoutTemplate>
</asp:Login>
<p>
Register
if you don't have an account.
</p>
</section>
Please help me :)

UserName is inside Login control, so you need to access it via Login1 control.
string userName = Login1.UserName;
string password = Login1.Password;
Another problem in your code is, you should not use loginButton_Click event if you use Login control.
In stead, you need to use the following depending on your satiation -
LoggingIn
LoggedIn

C# is case sensitive.
it should be Username with a capital "U" as per your control ID:
<asp:TextBox runat="server" ID="UserName" />
your code should be:
protected void loginButton_Click(object sender, EventArgs e)
{
if(FormsAuthentication.RedirectFromLoginPage(UserName.Text, Password.Text)
{
}
}
EDIT - Noticed the control in question is within a usercontrol so the code should now be:
protected void loginButton_Click(object sender, EventArgs e)
{
if(FormsAuthentication.RedirectFromLoginPage(Login1.UserName.Text, Password.Text)
{
}
}
however you still did have a typo in the UserName ID

Code is case sensitive.
if(FormsAuthentication.RedirectFromLoginPage(UserName.Text, Password.Text)
{
}

Related

Logout linkbutton is not firing click event

I am trying to implement my login and logout through my webpage. There are two panels which toggle. On my page load, the logout panel is made invisible so that the user may only see the login link. After logging in the login panel is made invisible and the logout panel is made visible where the logout link is there for logging out. but the logout linkbutton is not even firing the click event. This is what i see in the browser window at the bottom when i try to click the logout link button
This is my aspx page-
<div>
<asp:Panel ID="LoginPanel" runat="server">
<ul class="style1">
<li>Username</li>
<li><asp:TextBox ID="LoginEmail" runat="server" Width="90%" placeholder="Email" class="form-control"></asp:TextBox></li>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Email Required" ControlToValidate="LoginEmail" ForeColor="#CC0000" Font-Italic="True"></asp:RequiredFieldValidator>
<li>Password</li>
<li><asp:TextBox ID="LoginPassword" runat="server" Width="90%" placeholder="Password" class="form-control" TextMode="Password"></asp:TextBox></li>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Password Required" ControlToValidate="LoginPassword" ForeColor="#CC0000" Font-Italic="True"></asp:RequiredFieldValidator>
<li><asp:Button ID="LoginButton" runat="server" Text="Login" class="btn btn-default" onclick="LoginButton_Click"/>
New User?<asp:HyperLink ID="new_user" runat="server" NavigateUrl="Registration.aspx">Register Here</asp:HyperLink></li>
</ul>
</asp:Panel>
<asp:Panel ID="LogoutPanel" runat="server">
<asp:LinkButton ID="LogoutLink" runat="server" Text="Logout"
onclick="LogoutLink_Click" />
</asp:Panel>
</div>
This is my cs code-
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.Data;
using System.Configuration;
public partial class UserMasterPage : System.Web.UI.MasterPage
{
ConnectionClass cl;
SqlDataReader dr;
string sql;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LogoutPanel.Visible = false;
cl = new ConnectionClass();
}
}
protected void LoginButton_Click(object sender, EventArgs e)
{
sql = "select Id, Email_, Password_, Block from MemberLogin where Email_='" + LoginEmail.Text + "'";
cl = new ConnectionClass();
cl.establishConnection();
cl.createReaderCommand(sql);
dr = cl.executeReaderCommand();
if (dr.HasRows)
{
if (LoginPassword.Text == dr["Password_"].ToString())
{
if (dr["Block"].ToString() == "False")
{
Session["user"] = dr[0].ToString();
LoginPanel.Visible = false;
LogoutPanel.Visible = true;
//Response.Write("login successful");
}
}
}
cl.closeConnection();
}
protected void LogoutLink_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("AdminLogin.aspx");
}
}
This is on my master page. I need to solve this problem very soon. Please help me with this.Thanks in advance.
The __DoPostBackWithOptions call associated with the LinkButton is related to the presence of the validators in your page. Setting CausesValidation to false would replace it by the "regular" __doPostBack:
<asp:LinkButton ID="LogoutLink" runat="server" CausesValidation="false" ... />
The validators shown in your markup being in a Panel with Visible = false, they should not be active. But since I don't see anything else that could prevent the postback, turning off the validation for the LinkButton would at least eliminate that possible cause.
By the way, if you have many controls and validators in your form, you may consider grouping them with validation groups. That would allow each button (or other controls causing a postback) to trigger only the relevant validators:
<asp:TextBox ID="txt1a" runat="server" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="rfv1a" runat="server" ControlToValidate="txt1a" ValidationGroup="Group1" Text="*" ForeColor="Red" />
<asp:TextBox ID="txt1b" runat="server" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="rfv1b" runat="server" ControlToValidate="txt1b" ValidationGroup="Group1" Text="*" ForeColor="Red" />
<asp:Button ID="btn1" runat="server" Text="Button1" ValidationGroup="Group1" />
<br />
<asp:TextBox ID="txt2a" runat="server" ValidationGroup="Group2" />
<asp:RequiredFieldValidator ID="rfv2a" runat="server" ControlToValidate="txt2a" ValidationGroup="Group2" Text="*" ForeColor="Red" />
<asp:TextBox ID="txt2b" runat="server" ValidationGroup="Group2" />
<asp:RequiredFieldValidator ID="rfv2b" runat="server" ControlToValidate="txt2b" ValidationGroup="Group2" Text="*" ForeColor="Red" />
<asp:Button ID="btn2" runat="server" Text="Button2" ValidationGroup="Group2" />
In the example above, a click on btn1 would cause a postback even if txt2a is empty, because they don't belong to the Group1 validation group.

ASP.net Change Password Validator

I am working on a eCommerce project which has an admin panel and shopping panels.
I have finished programming and now testing every single aspx and cs files by manually.
The problem is, I have a change password feature which is related with session and Database. The problem is I have a validators in my aspx file but they won't work. Here my codes are;
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChange">
<div class="userForm">
<div class="formTitle">
Change Your Password
</div>
<div class="formContent">
<asp:TextBox ID="txtPassword" placeholder="Type your new password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtPassword"
ErrorMessage="RequiredFieldValidator" ForeColor="Red" Display="Dynamic" ValidationGroup="signup">Enter a password</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="txtAgainPassword" placeholder="Repeat your new password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" BorderColor="Red"
ControlToValidate="txtAgainPassword" Display="Dynamic" ErrorMessage="Enter password again."
ForeColor="Red" ValidationGroup="signup"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword"
ControlToValidate="txtAgainPassword" Display="Dynamic" ErrorMessage="Password don't match."
ForeColor="Red" ValidationGroup="signup"></asp:CompareValidator>
<br />
<asp:Button ID="btnChange" runat="server" Text="Submit" OnClick="btnChange_Click" />
<br />
<asp:Label ID="lblError" Visible="False" ForeColor="Green" runat="server"></asp:Label></div>
</div>
</asp:Panel>
</asp:Content>
and the .cs part is below
protected void btnChange_Click(object sender, EventArgs e)
{
using (ZirveMarketDBEntities context = new ZirveMarketDBEntities())
{
// Atanan sessiona gore user bilgisini al - guvenlik icin onemli
int id = (int)Session["CustomerID"];
Customer cust = context.Customers.Where(i => i.CustomerID == id).FirstOrDefault();
cust.Password = txtPassword.Text;
context.SaveChanges();
}
lblError.Visible = true;
lblError.Text = "Password successfully updated";
}
The problem is, I have a 2 box for new password and type new password. Even if they are null, even if they don't match the password still changes with the value of the first part. I don't want to run server side code if they don't match or null. What am I doing wrong? Helps are pretty apreciated.
Add the 'ValidationGroup="signup"' attribute to the btnChange button.
I'd also recommend adding the below to the click event (before anything else) in case Javascript is disabled on the client:
Page.Validate("signup");
if (!Page.IsValid)
{
return;
}
You have validation groups specified on the validators, but not on the Button. Try adding the validation group to the button as well.

How do I Make a message form in ASP.NET in C# without using a form

Ok this is what I have so far. I dont know where to put my email address and its not thowing any errors. it keeps failling the message.
my content.aspx page:
<%# Page Title="Contact" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="JeremiahTorresportfoliov1.Contact"%>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<hgroup class="title">
<h2>How to get in touch</h2>
</hgroup>
<section class="contact">
<header>
<h3>Phone:</h3>
</header>
<p>
<span>863.307.1652</span>
</p>
</section>
<section class="contact">
<header>
<h3>Email:</h3>
</header>
<p>
<span>Jeremiah#Leftclique.com</span>
</p>
</section>
<section class="contact">
<header>
<h3>Address:</h3>
</header>
<p>
5337 N. Socrum Loop Rd. #208<br />
Lakeland, FL 33809
</p>
</section>
<%# Register Src="~/MyUserControl.ascx" TagPrefix="uc1" TagName="MyUserControl" %>
<uc1:MyUserControl runat="server" id="MyUserControl" />
</asp:Content>
and this is my contact.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.ComponentModel;
namespace JeremiahTorresportfoliov1
{
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
MyUserControl.aspx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="JeremiahTorresportfoliov1.MyUserControl" %>
<div class="focus" >
<label>
I need...</label>
<asp:DropDownList ID="cboConsultationType" runat="server" CssClass="select sub web">
<asp:ListItem Value="I Need A New Web Site">A completely new website</asp:ListItem>
<asp:ListItem Value="Web Site Upgrade">My website upgraded</asp:ListItem>
<asp:ListItem Value="Application Design">An application </asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</div>
<ul>
<asp:Label EnableViewState="false" ID="lblErrorMessage" runat="server"></asp:Label>
<li>
<asp:Label EnableViewState="false" ID="lblName" AssociatedControlID="txtName" runat="server"
Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" ValidationGroup="ContactValidation"> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtName" ErrorMessage="Name is required">* </asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblPhone" runat="server" AssociatedControlID="txtPhone" EnableViewState="false"
Text="Phone"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server" ValidationGroup="ContactValidation"> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPhone" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtPhone" ErrorMessage="Phone is required">* </asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" EnableViewState="false"
Text="Email"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" ValidationGroup="ContactValidation"> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtEmail" ErrorMessage="Email is required">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="ContactValidation" ErrorMessage="Email address is invalid">*</asp:RegularExpressionValidator>
</li>
<li>
<asp:Label ID="lblMsg" runat="server" AssociatedControlID="txtMsg" EnableViewState="false"
Text="How can we assist you?"></asp:Label>
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" Rows="5" Wrap="true"> </asp:TextBox>
</li>
<li>
<asp:Button ID="btnSubmit" runat="server" EnableViewState="false" CssClass="submit"
Text="Send" ValidationGroup="ContactValidation" OnClick="btnSubmit_Click" />
</li>
</ul>
MyUserControl.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.ComponentModel;
namespace JeremiahTorresportfoliov1{
public class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool bSent = false;
try
{
//create the email and add the settings
var email = new MailMessage();
email.From = new MailAddress(FromEmail);
email.To.Add(new MailAddress(FromEmail));
email.Subject = Subject;
email.IsBodyHtml = true;
//send the email
var smtpServer = new SmtpClient();
smtpServer.Send(email);
//mark as sent ok
bSent = true;
}
catch (Exception ex)
{
//send any errors back
//add your own custom handling of errors;
}
//let the end user know if it was a success
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + (bSent ? SuccessText : FailureText) + "');", true);
}
//properties
public string FromEmail
{
get { return _fromEmail; }
set { _fromEmail = value; }
}
public string Subject
{
get { return _subject; }
set { _subject = value; }
}
public string SuccessText
{
get { return _successText; }
set { _successText = value; }
}
public string FailureText
{
get { return _failureText; }
set { _failureText = value; }
}
//fields
private string _fromEmail = "lostdestany#aol.com";
private string _subject = "Website Enquiry";
private string _successText = "Thank you for submitting your details we will be in touch shortly.";
private string _failureText = "There was a problem submitting your details please try again shortly.";
}
}
You can't inherit a form from a control, it should be from System.Web.UI.Page. Add a usercontrol and move all markup and code to that control:
MyUserControl.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="JeremiahTorresportfoliov1.MyUserControl" %>
<div class="focus" >
<label>
I need...</label>
<asp:DropDownList ID="cboConsultationType" runat="server" CssClass="select sub web">
<asp:ListItem Value="I Need A New Web Site">A completely new website</asp:ListItem>
<asp:ListItem Value="Web Site Upgrade">My website upgraded</asp:ListItem>
<asp:ListItem Value="Application Design">An application </asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</div>
<ul>
<li>
<asp:Label EnableViewState="false" ID="lblErrorMessage" runat="server"></asp:Label>
</li>
<li>
<asp:Label EnableViewState="false" ID="lblName" AssociatedControlID="txtName" runat="server"
Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtName" ErrorMessage="Name is required">*</asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblPhone" runat="server" AssociatedControlID="txtPhone" EnableViewState="false"
Text="Phone"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPhone" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtPhone" ErrorMessage="Phone is required">*</asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" EnableViewState="false"
Text="Email"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtEmail" ErrorMessage="Email is required">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="ContactValidation" ErrorMessage="Email address is invalid">*</asp:RegularExpressionValidator>
</li>
<li>
<asp:Label ID="lblMsg" runat="server" AssociatedControlID="txtMsg" EnableViewState="false"
Text="How can we assist you?"></asp:Label>
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" Rows="5" Wrap="true"></asp:TextBox>
</li>
<li>
<asp:Button ID="btnSubmit" runat="server" EnableViewState="false" CssClass="submit"
Text="Send" ValidationGroup="ContactValidation" OnClick="btnSubmit_Click" />
</li>
</ul>
MyUserControl.ascx.cs:
namespace JeremiahTorresportfoliov1
public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool bSent = false;
try
{
//create the email and add the settings
var email = new MailMessage();
email.From = new MailAddress(FromEmail);
email.To.Add(new MailAddress(FromEmail));
email.Subject = Subject;
email.IsBodyHtml = true;
//build the body
var sBody = new StringBuilder();
sBody.Append("<strong>Contact Details</strong><br /><br />");
sBody.AppendFormat("Needs: {0}<br />", cboConsultationType.SelectedValue);
sBody.AppendFormat("Name: {0}<br />", txtName.Text);
sBody.AppendFormat("Email: {0}<br />", txtEmail.Text);
sBody.AppendFormat("Number: {0}<br />", txtPhone.Text);
sBody.AppendFormat("Comment: {0}<br />", txtMsg.Text);
email.Body = sBody.ToString();
//send the email
var smtpServer = new SmtpClient();
smtpServer.Send(email);
//mark as sent ok
bSent = true;
}
catch (Exception ex)
{
//send any errors back
//add your own custom handling of errors;
}
//let the end user know if it was a success
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + (bSent ? SuccessText : FailureText) + "');", true);
}
//properties
public string FromEmail
{
get { return _fromEmail; }
set { _fromEmail = value; }
}
public string Subject
{
get { return _subject; }
set { _subject = value; }
}
public string SuccessText
{
get { return _successText; }
set { _successText = value; }
}
public string FailureText
{
get { return _failureText; }
set { _failureText = value; }
}
//fields
private string _fromEmail = "info#example.com.au";
private string _subject = "Website Enquiry";
private string _successText = "Thank you for submitting your details we will be in touch shortly.";
private string _failureText = "There was a problem submitting your details please try again shortly.";
}
}
}
Add this control to your page and fix code.
Contact.aspx:
<%# Page Title="Contact" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="JeremiahTorresportfoliov1.Contact" %>
<%# Register Src="~/MyUserControl.ascx" TagPrefix="uc1" TagName="MyUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<uc1:MyUserControl runat="server" id="MyUserControl" />
</asp:Content>
Contact.aspx.cs:
namespace JeremiahTorresportfoliov1
{
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
UPDATE: I can see in your update you still have error. Change this in MyUserControl.ascx.cs:
public class MyUserControl : System.Web.UI.UserControl
to this:
public partial class MyUserControl : System.Web.UI.UserControl
In Contact.aspx move reference to Myusercontrol to top of the page like this:
<%# Page Title="Contact" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="JeremiahTorresportfoliov1.Contact"%>
<%# Register Src="~/MyUserControl.ascx" TagPrefix="uc1" TagName="MyUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

<asp Login> control, cant find where username and password gets set with this control

I'm working on a project thats already build and working fine.What I need to do now is to encrypt the password with salt.Normally I can do this and all, but I'm struggling to see where the control asp:Login gets the username and password.I have searched all over the project and cant find any details to where the username and password gets retrieved from the Database and where it checks if it is correct.Here are the markup:
<asp:Login ID="LoginUser" runat="server" EnableViewState="False" RenderOuterTable="False" OnLoggingIn="LoginUser_LoggingIn">
<LayoutTemplate>
<div class="accountInfo">
<fieldset class="Login">
<p>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" CssClass="text"></asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" CssClass="text"></asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
</fieldset>
<span class="error">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="error"
ValidationGroup="LoginUserValidationGroup" />
<p class="submitButton">
<asp:Button CssClass="submitButton" ID="LoginButton" runat="server" CommandName="Login"
ValidationGroup="LoginUserValidationGroup" Text="d" OnClick="LoginButton_Click" />
</p>
</div>
</LayoutTemplate>
</asp:Login>
and here are the code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mycompany.BaseCode;
using System.Threading;
namespace mycompany.Account
{
public partial class Login : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
//RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(SessionManager.Culture);
lblLoginTitle.Text = Resources.Login.LoginTitle;
lblExtraInfo.Text = Resources.Login.ExtraInfo;
var llb = (Label)LoginUser.FindControl("UserNameLabel");
llb.Text = Resources.Login.Username;
llb = (Label)LoginUser.FindControl("PasswordLabel");
llb.Text = Resources.Login.Password;
LoginUser.FailureText = Resources.Login.FailureText;
var rfv = (RequiredFieldValidator)LoginUser.FindControl("PasswordRequired");
rfv.ErrorMessage = Resources.Login.PasswordRequired;
rfv = (RequiredFieldValidator)LoginUser.FindControl("UserNameRequired");
rfv.ErrorMessage = Resources.Login.UsernameRequired;
var btn = (Button)LoginUser.FindControl("LoginButton");
btn.Text = Resources.Login.btnLogin;
}
}
}
There is no OnLoggingIn="LoginUser_LoggingIn" or OnClick="LoginButton_Click" events anywhere to be found.I even looked in the base code.The funny thing is it is working.
Any explanation or tips of where to find these hidden details will be deeply appreciated
It will use the default MembershipProvider configured in your web.config if you don't have any event handlers.
If you haven't configured a MembershipProvider, I think you need to handle at least the Login.Authenticate event.

ASP.NET page freezing

So, I'm making an ASP.NET page in C#. I have a very simple form with 2 textboxes and 3 buttons on it. When I click a button for 'submit' it makes a call to an SQL Server database to retrieve some info. Once the data's retrieved, I have an if statement to check one of the loaded values. The problem is that the page seems to freeze after the button is clicked, and I can't click it again. I can still enter data in the textboxes, but the button doesn't show up as a LinkButton, the mouse icon doesn't change or anything. The code for the ASPX.CS page is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CharacterSheet.BLL;
using CharacterSheet.Data;
public partial class Login : System.Web.UI.Page
{
protected void ClearButton_Click(object sender, EventArgs e)
{
UserBox.Text = "";
PassBox.Text = "";
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
PlayerController pc = new PlayerController();
Player player;
if (UserBox.Text.Contains('#') && UserBox.Text.Contains(".c"))
player = pc.GetByEmail(UserBox.Text);
else
player = pc.GetByUser(UserBox.Text);
if (player != null)
{
if (!player.Flagged)
{
if (PassBox.Text != player.Password)
{
ErrorLabel.Text = "Password does not match our records. Please retype carefully...";
player.LoginAttempts++;
if (player.LoginAttempts >= 3)
player.Flagged = true;
pc.Update(player);
}
else
Response.Redirect(SiteData.LoginMainPage);
}
else
ErrorLabel.Text = "Your account has been flagged. Please e-mail our support team.";
}
else
ErrorLabel.Text = "Unable to find user. Please retype carefully...";
}
}
And the code for the ASP markup page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<style type="text/css">
.leftColumn
{
text-align: right;
}
.style1
{
width: 30%;
text-align: right;
height: 47px;
}
.style2
{
width: 70%;
text-align: right;
height: 47px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<table>
<tr>
<td class="leftColumn">
<asp:Label ID="Label1" runat="server" CssClass="FormText"
Text="Username or Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="UserBox" runat="server" ToolTip="Enter your username here"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="UserBox" ErrorMessage="Username or Email is required"
ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="leftColumn">
<asp:Label ID="Label2" runat="server" CssClass="FormText" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="PassBox" runat="server" TextMode="Password"
ToolTip="Enter your password here" TabIndex="1"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Password is required" ForeColor="Red"
ControlToValidate="PassBox">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1"></td>
<td class="style2">
<asp:ValidationSummary ID="ValidationSummary" runat="server"
CssClass="ValidationSumary" ForeColor="Red" Height="43px"
style="text-align: left" Width="335px" DisplayMode="List" />
<asp:LinkButton ID="SubmitButton" runat="server" CssClass="FormButton"
TabIndex="2" onclick="SubmitButton_Click" PostBackUrl="~/Login.aspx">Submit</asp:LinkButton>
<asp:LinkButton ID="ClearButton" runat="server" CausesValidation="False"
CssClass="FormButton" TabIndex="3" onclick="ClearButton_Click">Clear</asp:LinkButton>
<asp:LinkButton ID="NewAcctButton" runat="server" CausesValidation="False"
CssClass="FormButton" TabIndex="4">Create Account</asp:LinkButton>
<br />
<asp:Label ID="ErrorLabel" runat="server" ForeColor="Red"
CssClass="FormErrorLabel"></asp:Label>
</td>
</tr>
</table>
</asp:Content>
Thanks to anyone who can help with this! :-)
EDIT: Upon further investigation (commenting out lines of code sequentially), I find that the problem arises when I update "ErrorLabel.Text". If I don't do that, then it functions just fine...this is confusing me a lot now...
If there is any unwanted code in aspx page or runtime error came then the page will freeze.To avoid this analyze your code once again and remove unwanted things it will run.
After randomly clicking buttons in the properties of the label, I find it stops breaking when I don't attach a CSS class to the label...I have no idea why, but it fixed the problem.

Categories