Ghaleon
Ghaleon

Reputation: 1196

Assign a value from database to textbox with TextMode = Password

I have an asp:TextBox with TextMode = "Password", and its working fine when I need to type on it. My textbox:

<asp:TextBox ID="Txt_NovaSenha" runat="server"  TextMode="Password" Width="170px"></asp:TextBox>

But now I need to get the password from the database and assign it to the TextBox.Text, like:

DataSet ds = forza.BuscaDadosCadastrais(Session["login"].ToString());

   foreach (DataRow row in ds.Tables[0].Rows)
     {
        TxtNome.Text = row["nome"].ToString();
        TxtEmail.Text = row["email"].ToString();
        TxtLogin.Text = row["login"].ToString();
        string aux = crip.DescriptografaString(row["senha"].ToString());
        Txt_SenhaAtual.Text = aux;
        Txt_SenhaAtual.TextMode = TextBoxMode.Password;
     }   

But when it's with TextMode = Password. The textbox gets empty.
And when I remove this part, it works fine...
Is there a way to work around it ?

Upvotes: 1

Views: 6027

Answers (4)

Zo Has
Zo Has

Reputation: 13018

You don't have to show the passwords at all. On your edit user page, you can show them a dummy password like this (don't replace the asteriks with the actual password as they can be seen if you view page source in browser)

Txt_SenhaAtual.Attributes.Add(value,"******");

Set the textmode of the textbox as "Password". When the user enters/updates their password they would still see "*". You can include a confirm password textbox & compare the 2 values on update.

Upvotes: 1

Only You
Only You

Reputation: 2072

You don't have to assign the password value from database to the password textbox in your "Edit Registration Data" form, users can't see the password text any way, why you want to show it?

When users edit their data the Password and Confirm Password textboxes in your edit form should show empty.

If users want to edit/change their existing password, they just type it in and confirm it, and save it; on saving IF users entered a password you will replace the existing password with the new one.

If users don't want to edit/change their password THEY DON'T ENTER ONE and you don't replace the existing password when saving.

You can bring all the other data your users CAN ACUTALLY SEE from the database except for the password.

You should/can remove these lines from your code:

string aux = crip.DescriptografaString(row["senha"].ToString());
Txt_SenhaAtual.Text = aux;
Txt_SenhaAtual.TextMode = TextBoxMode.Password;

For your reference the answer below explains why the ASP.NET textbox with TextMode="Password" is empty on postback and, how you could force a text value to be sent back to the browser:

ASP:TextBox Value disappears in postback only when password

Upvotes: 3

jason
jason

Reputation: 3615

You will have to programmatically change the textMode, like this:

Me.Txt_NovaSenha.TextMode = TextBoxMode.SingleLine
Me.Txt_NovaSenha.Text = "test"

But, of course, this goes against the purpose of the password mode. From a security standpoint, your passwords should be hashed and unrecoverable. Think about how email sites you use work. If you forget your password, they dont give it to you, they make you reset it. That's because passwords are hashed and cannot (should not) be un-hashed.

If you are just looking to have a watermark, you can use the ajaxcontrol toolkit.

 <ajax:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="Txt_NovaSenha" WatermarkText="****">
    </ajax:TextBoxWatermarkExtender>
<asp:TextBox ID="Txt_NovaSenha" runat="server"  TextMode="Password" Width="170px"></asp:TextBox>

Just make sure you download and include the toolkit dll (http://ajaxcontroltoolkit.codeplex.com/)

Upvotes: 1

Brad M
Brad M

Reputation: 7898

Depending on the scenario...

// don't use an asp textbox
<input type="password" value="<asp:Literal id="passwordText" runat="server" />" />

Or, as a more complicated solution, parse your outbound html looking for the input element and replace the empty string with the actual password.

Upvotes: 1

Related Questions