Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Ask User for a Password which is then stored in a SecureString

In the small application that I'm currently developing for a customer I need to ask the user for his windows login username, password and domain and then use those with System.Diagnostics.Process.Start to start an application.

I have a textbox with UseSystemPasswordChar to mask the entered password.

I need a System.Security.SecureString to feed the password to System.Diagnostics.Process.Start.

How do I convert the entered text to secure string while not doing it one character after another? Alternatively: Is there a better window control to ask the user for a password that returns the entered text as SecureString?

like image 403
BlaM Avatar asked Nov 25 '08 15:11

BlaM


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


2 Answers

Try looking at the SecurePasswordTextBox custom control. Are you trying to do something similar to a "Run As" type command where you are trying to run the process as a different user than the one currently logged on? If not, you should be able to just call Process.Start and let it pick up the current users credentials.

Also, take a look at the following resources as well:

  • CredUIPromptForCredentials
  • CredUIPromptForWindowsCredentials (Vista and Server 2008)
  • Microsoft Support article
  • LogonUser Function
  • LogonUserEx Function
  • CreateProcessAsUser
  • CreateProcessWithLogonW
  • CreateProcessWithTokenW

The best option would probably be to use some interop p/inovke code to call CredUIPromptForCredentials to display the standard Windows dialog box and then use that information to either call Process.Start, or, more likely, call the CreateProcessAsUser function.

like image 197
Scott Dorman Avatar answered Sep 22 '22 13:09

Scott Dorman


The reason the SecureString wants to accept one character at a time is that otherwise you would have the entire string before that and cause the string to be in memory. Thus, using a SecureString in this scenario kind of defeats the purpose.

like image 27
configurator Avatar answered Sep 22 '22 13:09

configurator