I've noticed recently that I began logging in to my dotText admin area sometimes every few hours, sometimes after a day or two. Like a bad Methodist I thought I could fix it in my code, but smart developers know that when something just stops working it often has nothing to do with their code. Forms authentication was persisting just fine on my development server, thanks, but not where it counted, here at dbvt.com/blog hosted by WebHost4Life. So I open a ticket and deal with well-intentioned, Junior tech support people asking for a URL so they can replicate the problem on their end. "What's to replicate? Forms authentication doesn't persist." Insisting on keeping the ticket open for observation, a week later it apparently gets bumped up to the one or two WH4L techs who can help me.
Seems the application pool in a [heavily] shared hosting environment like WH4L is reset regularly, requiring me to re-authenticate.
"...please keep in mind that if the session reloads every 1-2 days that's perfectly normal because the application pool does have to be resetted every 12 hours."
Okay, now we're getting somewhere. Weird that it persisted for weeks on end until a few days ago, but it was an answer I could live with. Once again, I have nothing bad to say about WH4L. I recommend them. You get what you pay for, of course, and I continue to feel I'm getting a good value.
About the quick-n-dirty fix, I got real tired, real quick typing in my username and password, so I'm pre-populating them. A simple click [OK] and I'm happily in my secret blogging chamber. Here's the code if helpful to anyone else in a similarly dirty application pool.
private void Page_Load(object sender, System.EventArgs e)
{
HttpCookie cookie = Request.Cookies["dbvtadmin"];
if (cookie != null)
{
tbUserName.Text = cookie.Values["username"];
tbPassword.Text = cookie.Values["password"];
}
}
private void lblLogin_Click(object sender, System.EventArgs e)
{
BlogConfig config = Config.CurrentBlog(Context);
if(Security.Authenticate(tbUserName.Text,tbPassword.Text,true))
{
SaveAccountToCookie();
if(Request.QueryString["ReturnURL"] != null)
Response.Redirect(Request.QueryString["ReturnURL"]);
else
Response.Redirect(config.FullyQualifiedUrl + "admin/default.aspx");
}
else
Message.Text = "That's not it
";
}
private void SaveAccountToCookie()
{
// Create or update cookie to populate username and password fields on future visits
HttpCookie cookie = new HttpCookie("dbvtadmin");
cookie.Expires = DateTime.MaxValue;
if (cookie == null)
{
cookie.Values.Add("username", tbUserName.Text);
cookie.Values.Add("password", tbPassword.Text);
Response.Cookies.Add(cookie);
}
else
{
cookie.Values["username"] = tbUserName.Text;
cookie.Values["password"] = tbPassword.Text;
Response.Cookies.Set(cookie);
}
}