Reputation: 51
I created a 'Remember me' option on an ASP site and the client said they also wanted it to show the login name in the user field even after they press 'logout'. It works as long as I don't ever use a different login name again. Once I assign the value to the text box in the code behind, even if I manually type a new value the old value is what is used.
Example:
No matter what I do, it will not change once I set it in the codebehind file. It's almost as if once the text field is set, it cannot be changed. This is not right, but I have no good explanation for it.
Here is the code in question:
Page load:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.ServerVariables["AUTH_USER"] != null && !Request.ServerVariables["AUTH_USER"].Equals(""))
{
login.Visible = false;
logout.Visible = true;
btnLogout.Text = "Logout " + Request.ServerVariables["AUTH_USER"];
}
else
{
login.Visible = true;
logout.Visible = false;
CheckLoginCookie();
}
}
Code to set the cookie:
private void SaveLoginCookie()
{
try
{
Response.Cookies["KYSUSR"].Value = txtLoginUsername.Text.Trim();
Response.Cookies["KYSUSR"].Expires = DateTime.Today.AddMonths(6);
}
catch (Exception ex)
{
ExceptionHandling.SendErrorReport(this, ex);
}
}
Code to load the cookie:
private void CheckLoginCookie()
{
try
{
if (Request.Browser.Cookies)
{
if (Request.Cookies["KYSUSR"] != null && Request.Cookies["KSYFOR"] != null)
{
// logged in as remember
if (Request.Cookies["KYSFOR"].Value == "R")
txtLoginUsername.Text = Request.Cookies["KYSUSR"].Value;
// once set here, the Text property never changes regardless of what is entered into it
}
}
}
catch (Exception ex)
{
ExceptionHandling.SendErrorReport(this, ex);
}
}
Code to do the login:
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
String user = txtLoginUsername.Text.Trim();
if (chkSaveUser.Checked)
SaveLoginCookie();
else
{
// set cookie as expired so browser will clear it
Response.Cookies["KYSUSR"].Expires = DateTime.Today.AddDays(-1);
}
if (CheckLogin(user, txtLoginPassword.Text))
{
if (chkSaveUser.Checked)
{
FormsAuthentication.SetAuthCookie(user, true);
}
FormsAuthentication.RedirectFromLoginPage(txtLoginUsername.Text.Trim(), false);
}
}
catch (Exception ex)
{
ExceptionHandling.SendErrorReport(this, ex);
}
}
Why would the Text property not change?
Upvotes: 2
Views: 1053
Reputation: 159905
The issue is that you don't check if you are in a postback in your Page_Load
method - which means even when the user puts something else in the txtLoginUsername
Textbox it is overridden by CheckLoginCookie
.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.ServerVariables["AUTH_USER"] != null
&& !Request.ServerVariables["AUTH_USER"].Equals(""))
{
login.Visible = false;
logout.Visible = true;
btnLogout.Text = "Logout " + Request.ServerVariables["AUTH_USER"];
}
else
{
login.Visible = true;
logout.Visible = false;
// Only check the login cookie if we are not dealing with a form submission.
if (!Page.IsPostBack)
{
CheckLoginCookie();
}
}
}
Upvotes: 3