Thursday, May 21, 2009

One Step at a time - Changing a password

If in asp.net you want to change a password but don't know the current password you need to generate a new one and use it to change the password. So to change a users password to "abc123"

MembershipUser mUser = Membership.GetUser(username);
string tmpPassword = mUser.ResetPassword();
bool PassswordChanged = mUser.ChangePassword(tmpPassword, "abc123");
because
mUser.SetPassword( "abc123");
Just didn't makes sense. Don't tell me what to do I'll write my own policies.

Friday, May 15, 2009

Well that’s weird – hidden validation controls

This is the first of a hopefully regular column. In it I’d like to cover weird bits of code that just don’t make sense and I haven’t seen elsewhere but are required to make something work. It is my hope that others can learn from my pain.

Today I’m going to cover server validation. Asp.net has a very handy custom validation control that can be used to check values preventing things like duplicates in a database. I won’t go into it as more information can be found here

http://www.4guysfromrolla.com/articles/073102-1.aspx

My issue came in that I have three groups of controls. Group one, two and a general group. The button you clicked determined if group one or two was validated but the rest where validated with a line of code like this.

Page.Validate("general");

This worked great until I decided that I should hide my form by default and show it only if needed.

panForm.Visible = false;

With the panel containing the control hidden when I called Validate(“general”) it preformed no validation. This is because although the control existed asp.net assumed it was not to be used as it was hidden. My eventual solution was to get the hidden state of the panel and unhide it then rehide it.

bool formVisibility = panForm.Visible;
panForm.Visible = true;
Page.Validate("general");
panForm.Visible = formVisibility;

One Problem solved, many more to come.