Set password textbox value in a secure way - c#

Is there any secure way of setting value to textbox in password mode?
I have a custom login form with a password textbox:
<asp:TextBox ID="tbPassword" runat="server" TextMode="Password"></asp:TextBox>
and on page load, I'd like to put there some remembered password I've decrypted in code-behind from a cookie.
95% of answers I've found is to set the textbox value, like:
tbPassword.Attributes["value"] = "ThePassword";
However, it's not the most secure way, as it generates following HTML code, visible when view source, where password is stored in a plain text:
<input id="tbPassword" type="password" value="ThePassword" name="ctl00$cpMainContentParent$tbPassword">
I've tried different way with jQuery, setting value with:
$("#tbLogin").val("ThePassword");
It is much better, as the value is not visible in View Source, yet the password is visible in jQuery script on a page...
I've also tried to register client script and run it, but as I cannot "unregister" it, so result is the same as with the plain jQuery sript...
Do you know any workaround to set that value and not show it in source code?

As Bob's comment says, the whole concept is flawed. If the cookie is valid, then just skip the password prompt. If it's not valid, then you have nothing to auto-fill with.
That being said, there is nothing you can do to directly fill the textbox which can't be intercepted in some way. Even if you filled it via a post-load AJAX call, the user could still see the content via something like FireBug. That's inherent in the nature of web browsers - there's nothing that's secure from the browser itself, because the browser needs to understand it to render it.
If you really want to do this, one option is to compute a custom hash of the password which incorporates the timestamp (or some other one-time value), and set the textbox to that. You'd need to accept both the standard password and the hash as valid, but the hash won't be reusable so it won't matter if the user sees it.

Do not set the password in a INPUT element. Instead, set it to a random or fixed value (if you want to indicate a password is set) or empty otherwise. As you say, it means the password is visible in the page source. To handle changing the password, set a flag if the password value so the handling code, either server or client side, knows when the password is modified.

Related

Can i use PasswordRecovery Control ASP.NET (Membership) Without Wizard structure? Without SuccessTemplate and Question Template?

I am using the PasswordRecovery Control in my ASP.NET WebForms Application (C#), now the thing is, When we are using PasswordRecovery Control, we are forced to use Wizard Control, which means, if the user enters username correctly, it will then hide textbox and display success message (or whatever you put in SuccessTemplate).
Now, the issue is by this way, when Unauthorized user, tries to access the application, they can try this control to get the actual username from the application (security risk). So, if they try the wrong username, they will get the "UserNameFailureText" and if they will enter the proper username, they will see the next template (SuccessTemplate) which will show a success message (By this way, they can get that the entered username is available in the system or not), so I want to remove the wizard structure, in all scenario, the textbox with a username will stay on the screen, and no matter what user enters, he will see a generic message "if you have entered the correct username, you will receive an email" Like that.
If anyone has any idea whether it's possible in PasswordRecovery control, or should I have to build a custom Page?
PS: I have tried removing SuccessTemplate from the page, it will automatically take the default success template.
I don't see why you can't just create a page from scratch? All that text box will do is check if the user exists, send them the email, and display your message. There not a whole lot of reasons thus to use the built-in template.
So, a simple button re-set password can run some code behind, send the re-set email, and set a label or text box, or even some "div" as visible = true to display your message. You don't mention or note that authentication provider you are using - but given the built in templates - then that suggests FBA, and thus the tables that drive the site and hold user + passwords should be fully available to your code behind.
On the other hand, you might have to add some kind of password re-set table, and say include a GUID generated ID, and the datetime. That way, the link you send in the email is specific to the one user - and has a limited time before that link expires.
Or I suppose the link in the email just directs them to the new password page - but I tend to toss in a GUID that is checked against that new re-set table. With the guid, then when they click on their email link, you can display their name, and only prompt for the new password. The email link simply includes that GUID as a parameter, and you pull that one row from the re-set table to get who is about to re-set their password.

Insert text into a password field from form application

I'm working on a Windows form application where I want to automate some tasks. I'm have a problem when it comes to password fields.
My problem is when I press the submit button it says incorrect username/password. When I pass my text into the text fields like:
Username: hey
Password: hey
(So in the password field it's still plain text but it should have changed to ●●●)
I guess it's becuase I set the value, and that's why it only works with the username.
I have also tried:
element.InnerText = "hey";
Does anyone know how I can insert a valid password "text" like ●●● insert of plain text.
I don't know if it has any significance but there is also some javascript on the textfield (on the site I'm trying to login at).
Another thing is when i inspect the source I can see that the passwords field type="text"?
I have tried other automation software and I can see that they can't insert into the password field either. So perhaps there is some kind of blockage here?
I hope I make myself clear, else I would gladly explain in more details.
And I must say I have almost searched the entire web without any luck! :p
Thanks
Solved
The problem was that there was a textbox on top of the password field. I suddenly saw that after I looked in the source code again!
On the textbox on witch you type the password change the textbox.PasswordChar proporty to ●
textbox1.PasswordChar = '●';
The problem was that there was a textbox on top of the password field. I suddenly saw that after I looked in the source code again!
The thing there confused me was because I couldn't see any there were any text inserted. lol

how to show password in asterik format when we generate random password

i am generate random password and show it in a textbox.
when i am set textbox property textmode to password then it doesn't show in textbox but when i set it singleline then password shows in textbox.
I am using following code --
textbox1.attributes.add("value",passwordvalue);
for show i am using --
textbox1.text = textbox1.attributes["value"].tostring();
Same happing with when i edit record. password doesn't show in textbox.
When you set a textbox in password mode, it renders as an <input type="password" >, which hides what's written in it, but this also forbids setting the value of the field. A password field is strictly for users to type in, you can't pre-fill it with anything.
If you want a textbox that hides the characters, but still allows setting the initial value, you have to build it yourself (or find something that someone else built) using HTML and client script.
Does this help?
HTML
<body>
<input type="text" id="passbox" />
<input type="submit" onclick="generate_password()" value="Generate Password" />
<input type="submit" onclick="toggle_passbox()" value="Toggle Box" />
</body>
JAVASCRIPT
function passbox() {
return document.getElementById('passbox');
}
function generate_password() {
passbox().value=Math.random().toString(16).slice(2);
}
function toggle_passbox() {
passbox().type= passbox().type == "password" ? "text" : "password";
}
You can test this at http://jsbin.com/uxano3
textbox1.text = "yourrandompassword"
i dont get what you are trying to do with the attributes.
Are you using a asp control element for the textbox?
if you are generating a random password, i guess you want to show it to your user, not hide is with the asterix'es?
That approach doesn't make sense since even if it showed it would be just *****, and then that leaves the user in the same situation. The password field just isn't meant to be showed.
If you must keep with the generate random password, you could:
Show some simple text above the password field, something like: "Please enter your password in the field below. Alternatively leave it blank, and use this password we have generated for you: SomeNicePassword"
About the edit scenario, you shouldn't have passwords in clear, its just not safe and could upset some customers. Besides, an edit password feature usually requires the user to enter their current password and the new one in separate fields, to make sure it wasn't that it wasn't that the user left the session open and someone else is trying to take over the account.
If you want to use Vb.net you will have to use the following code for creating textbox as password
textbox1.passwordchar="*"
If you want to use C Sharp you will have to use the following code for creating textbox as password
textbox1.passwordchar='*';
If you want to use C++ you will have to use the following code for creating textbox as password
textbox->passwordchar='*';

How to prevent a password input from clearing after submit?

If you have a page with an <asp:TextBox TextMode="Password" ... />.
How can you keep the value after a postback?
This is my problem:
At the registration screen of my app you need to enter a password.
Then you click submit, a postback occurs and the password fields are cleared, how can I prevent the password field from clearing?
You require to set it again in page_load or in button click event like this :
string Password = txtPassword.Text;
txtPassword.Attributes.Add("value", Password);
You need to set back the password to the textbox on postback.
txtBox.Attributes["value"] = txtBox.Text;
Best way
dont set input type in aspx page,
set type of input in Pageload in !postback Section
txtPassword.Attributes["type"] = "password";
<input type="password" /> is treated differently than other form controls since it stores sensitive information that is a password of a user. At server side, for every postback the password textbox is force-fully cleared for this reason, should you really need to persist the value in password text-box, set it explicitly as others have mentioned here. But I really don't recommend doing so since it's not a good practice.
I realize this is an old post, but hopefully this will help someone else. I had the same issue on a user setup screen whereby I kept losing the password input during various postbacks. The solution I chose was to place portion of the input screen that posted back into an updatepanel. This solved the problem of the password being blanked out AND didn't present a security risk.
Hope this helps!
Use Jquery to retain your password after postback or submit
$(function () {
$('.txtPassword').val("<%=txtPassword.Value%>");
});
Just add type="password" in asp:textbox and remove Textmode="Password" and no need to write any code in code-behind.

How to get password html helper to render password on failed validation

I have a form for creating a new account and it has a password field in it. I'm using view models to pass data to the controller action and back to the form view. When the user enters their details in, and clicks submit, if validation fails and it returns them to the same view passing back in the view model, it won't default the password to what they entered.
How can I get it to do this? Or should I even try?
You can set the password after validation by explicitly calling:
<%= Html.Password("Pwd", Model.Pwd) %>
As for motivation I've seen both behaviours in the web, but probably I would choose to leave the password empty and make the user retype the password, hence leaving the Password Helper behaviour as it is.

Categories

Resources