By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

How to make the console hide passwords that are typed in

P: n/a
Hi,

I need to make a command line application that I have written support
an option that allows the user to use the /p:* switch to specify that
they would like to type a password but have it hidden so that each key
stroke generates '*' as its output, hiding the what was typed by the
user. If anyone knows how to do this in C# your help would be greatly
appreciated.

Thanks in advanced,

Ryan

Feb 6 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ryan,

Assuming you are using a TextBox for the password entry, just set the
'PasswordChar' property to a non-zero value...

Regards,
John Bendiksen

Feb 6 '06 #2

P: n/a
On 6 Feb 2006 15:02:44 -0800, "JohnAtAphelion"
<jb********@aphelion.net> wrote:
Ryan,

Assuming you are using a TextBox for the password entry, just set the
'PasswordChar' property to a non-zero value...

Regards,
John Bendiksen


There's no GUI in a console.

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 7 '06 #3

P: n/a
On 6 Feb 2006 13:42:22 -0800, ry*********@netiq.com wrote:
Hi,

I need to make a command line application that I have written support
an option that allows the user to use the /p:* switch to specify that
they would like to type a password but have it hidden so that each key
stroke generates '*' as its output, hiding the what was typed by the
user. If anyone knows how to do this in C# your help would be greatly
appreciated.

Thanks in advanced,

Ryan


Forgive me, it's late here so I won't be typing any sample code but
I'll get you started on what I would attempt as the solution. Others
my approach this differently so don't be surprised to see the right
answer in here with mine, hehehe ...

Read the character input one character at a time and put it in a char
array (computationally cheap) or append it to a string
(computationally expensive). As each char is read in output an '*' to
the console.

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 7 '06 #4

P: n/a
Thanks Ken,

That was the approach that I was trying to take but I am not sure how
to do that using C#. I was playing around with streams and the
Console.In and Console.Out properties but can't figure out how to
prompt the user for input but have that input redirected directly to me
before it pops up on the screen. If anyone is more familiar with
streams and System.Console please post up.

Ryan

Feb 7 '06 #5

P: n/a

"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
| Thanks Ken,
|
| That was the approach that I was trying to take but I am not sure how
| to do that using C#. I was playing around with streams and the
| Console.In and Console.Out properties but can't figure out how to
| prompt the user for input but have that input redirected directly to me
| before it pops up on the screen. If anyone is more familiar with
| streams and System.Console please post up.
|
| Ryan
|

You'll have to disble the console "Echo" and "Line" mode, there is no way to
do this using the framework so you have to PInvoke some Kernel32 API's.
API's you will need to call are:

1. GetStdHandle: This is needed to get the handle to the console input file
handle.
2. GetConsoleMode: used to get the current mode of the console input device.
and
3. SetConsoleMode: to change the mode (disable Echo mode and Line input
mode).
Now you can read character by character from keyboard input and display a *
for each char received.
When done you'll have to restore the previous mode, so you have to save the
mode in 2.

Please check MSDN for more details about the API's mentioned above.

Willy.

Feb 7 '06 #6

P: n/a
Thanks Willy. I can't believe the Console object doesn't have a
property for this type of thing.

Feb 7 '06 #7

P: n/a

"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Thanks Willy. I can't believe the Console object doesn't have a
| property for this type of thing.
|

It's very uncommon to do this in console style applications, don't forget
that when adding features to a class, you are automatically extending it's
memory consumption (both code and data), that means there is a tradeoff to
make - features and resource usage. Don't let people pay for what they don't
need in most cases.

Willy.
Feb 7 '06 #8

P: n/a
Your concern is much appreciated but there is definitely a need and I
can't imagine any performance trade offs that would override that need.
Also, my experience has been very different with applications needing
this feature. Without this type functionality how do you allow someone
to discretely enter their password in a console application?

Ryan

Feb 7 '06 #9

P: n/a
On Tue, 7 Feb 2006 17:07:00 +0100, "Willy Denoyette [MVP]"
<wi*************@telenet.be> wrote:

"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11*********************@g47g2000cwa.googlegr oups.com...
| Thanks Ken,
|
| That was the approach that I was trying to take but I am not sure how
| to do that using C#. I was playing around with streams and the
| Console.In and Console.Out properties but can't figure out how to
| prompt the user for input but have that input redirected directly to me
| before it pops up on the screen. If anyone is more familiar with
| streams and System.Console please post up.
|
| Ryan
|

You'll have to disble the console "Echo" and "Line" mode, there is no way to
do this using the framework so you have to PInvoke some Kernel32 API's.
API's you will need to call are:

1. GetStdHandle: This is needed to get the handle to the console input file
handle.
2. GetConsoleMode: used to get the current mode of the console input device.
and
3. SetConsoleMode: to change the mode (disable Echo mode and Line input
mode).
Now you can read character by character from keyboard input and display a *
for each char received.
When done you'll have to restore the previous mode, so you have to save the
mode in 2.

Please check MSDN for more details about the API's mentioned above.

Willy.

Thanks for the further input as I suspected I might not have the whole
picture. I haven't had to do any console programming for a while. It
makes me wonder why we couldn't have a mechanism as simple as
'cin'/'cout' though. [8-)

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 7 '06 #10

P: n/a
Well, I have to disagree, it's not because YOU need something that there is
a common need for it, especialy when you have the possibility to add the
feature through PInvoke (which is an important tool in managed code after
all). Also, keep in mind, you're not the only one which may have some
obscure feature it would like to see added to the framework, if you don't
take care you'll end with an unmanageable framework (too many API's),
containing a lot of features that are only needed by very few applications,
that would account for a large part of the already (too) large working set
of a managed application. Notice that the System.Console (mscorlib) class is
loaded into all types of managed applications.

Willy.
"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
| Your concern is much appreciated but there is definitely a need and I
| can't imagine any performance trade offs that would override that need.
| Also, my experience has been very different with applications needing
| this feature. Without this type functionality how do you allow someone
| to discretely enter their password in a console application?
|
| Ryan
|
Feb 7 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.