<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tutto .NET : ChangePassword</title><link>http://weblogs.asp.net/alessandro/archive/tags/ChangePassword/default.aspx</link><description>Tags: ChangePassword</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Customizing the ChangePassword control and removing the required CurrentPassword field</title><link>http://weblogs.asp.net/alessandro/archive/2008/02/15/customizing-the-changepassword-control-and-removing-the-required-currentpassword-field.aspx</link><pubDate>Fri, 15 Feb 2008 15:47:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5794715</guid><dc:creator>alessandro</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alessandro/rsscomments.aspx?PostID=5794715</wfw:commentRss><comments>http://weblogs.asp.net/alessandro/archive/2008/02/15/customizing-the-changepassword-control-and-removing-the-required-currentpassword-field.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;It's very rare that what is already provided in asp.net under the Login controls fits my requirements out of the box without some tweaking. Not that any of these controls offer anything specialized, but certainly they are a big time saver if we can re-utilize their functionality. &lt;/P&gt;
&lt;P mce_keep="true"&gt;First some background as to why i personally want to customize the ChangePassword control to suit my needs : &lt;/P&gt;
&lt;P mce_keep="true"&gt;Password recovery is what i was after today, however i have hashed passwords, and recovery is impossible. If the user lost their password, then there is no way for me to know what their password is and send it back in clear text. &lt;/P&gt;
&lt;P mce_keep="true"&gt;The ideal solution is to reset the password, however the autogenerated password is quite ugly and quite hard to remember. What I've decided to do is send the email during password recovery, but as part of the email, instead of telling the user their old password(which i can't) , I'm instead going to ask them to click on a tokenized link that will guarantee to me that they are indeed the ones that requested the password, send them to the page where they can provide a new password, in the background i'd be autogenerating a password first ofcourse, then updating the password with their new password because the MembershipUser.ChangePassword(oldPassword, newPassword) method requires Old password as one of it's two parameters.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;This password change step, i'd like to be done using the ChangePassword control, however to my big surprise CurrentPassword Field is a required field that i cannot remove. This is also a field that I do not want asked for during the password change request(since my user has forgotten their password and are now going to provide their new pasword).&lt;/P&gt;
&lt;P mce_keep="true"&gt;There is ofcourse no property or method in this control that removes the CurrentPassword field requirement, below is a screenshot of the ChangePassword control in designview, as you can note, the highlighted field is the CurrentPassword field i do not want. &lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alessandro/ChangePassword1.gif" mce_src="http://weblogs.asp.net/blogs/alessandro/ChangePassword1.gif"&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;I've done a quick look on google and in the asp.net/forums and didn't find anybody providing any proper solutions either,&amp;nbsp;mostly vague replies : &lt;A href="http://forums.asp.net/p/1189347/2038354.aspx"&gt;http://forums.asp.net/p/1189347/2038354.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;As you can read from the posts there, the issue seems to be two things which&amp;nbsp;were also my same issues&amp;nbsp;: &lt;BR&gt;1) Remove the current password label/TextBox &lt;BR&gt;2) Pass the new resetpassword to CurrentPassword Property which by the way is a getter only and not settable (SAD SAD)&lt;/P&gt;
&lt;P mce_keep="true"&gt;Both of these things are not supported in this control. So let's quickly fix requirement 1 and there are a couple of ways to fix this :&amp;nbsp;&amp;nbsp;&lt;BR&gt;a) You have to define a custom&amp;nbsp; &amp;lt;ChangePasswordTemplate&amp;gt;. This can be easily done by taking your ChangePassword control into DesignView in Visual studio, right click on the control and select "Convert to template". You can then switch to HtmlView and set the visibility of CurrentPasswordLabel, CurrentPassword and CurrentPasswordRequired controls.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;BR&gt;b) If you prefer to do this in code, then you can find the Label and TextBox for CurrentPassword and set its visiblity to false. Since a is a nobrainer, i'm including a sample code of method (b) : &lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #000000"&gt;Label l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (Label)changePassword1.ChangePasswordTemplateContainer.
    FindControl(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;CurrentPasswordLabel&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (l &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    l.Visible &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;false&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

TextBox tb &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (TextBox)changePassword1.ChangePasswordTemplateContainer.
FindControl(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;CurrentPassword&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (tb &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    tb.Visible &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;false&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;

RequiredFieldValidator rfv &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (RequiredFieldValidator)changePassword1.
ChangePasswordTemplateContainer.FindControl(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;CurrentPasswordRequired&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (rfv &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
    rfv.Visible &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;false&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Now that we have the fields we want disabled, let's head onto fix issue 2 : &lt;BR&gt;We can't pass the Autogenerated password to the CurrentPassword Property because its a getter only, however this getter returns the value from our CurrentPassword TextBox, and this job is done immidiately after ChangingPassword event fires. This is good news for us, so we can resolve issue 2 like this : &lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; changePassword1_ChangingPassword(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; sender, 
    LoginCancelEventArgs e)
{
    changePassword1.UserName &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; user.UserName;
        TextBox currentPassword &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (TextBox)changePassword1.
         ChangePasswordTemplateContainer.FindControl(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;CurrentPassword&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;);
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (currentPassword &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)
            currentPassword.Text &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; user.ResetPassword();
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Note that in the above code, user is a reference to a field of type&amp;nbsp;MembershipUser. Ok, that's it. Now we have what were after, look at the screenshot below :&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://weblogs.asp.net/blogs/alessandro/ChangePassword2.gif" mce_src="http://weblogs.asp.net/blogs/alessandro/ChangePassword2.gif"&gt;&lt;BR&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5794715" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alessandro/archive/tags/Login+Controls/default.aspx">Login Controls</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/ChangePassword/default.aspx">ChangePassword</category></item></channel></rss>