0

I designed a login form and wanted the username and password entered to be stored as variables I could call in other forms (overall program is a collection of .exe files and powershell scripts that I want to run as the username and password initially entered in my login form).

At the start of the program I created "global variables" using this code:

class usernameGlobalVariable
    {
        public static string var = "";
    }
    class passwordGlobalVariable
    {
        public static string var = "";
    }

In the login form I stored the username and password entered into these global variables this way:

private void usernameTextBox_TextChanged(object sender, EventArgs e)
    {
        usernameGlobalVariable.var = usernameTextBox.Text;
    }


    private void passwordTextBox_TextChanged(object sender, EventArgs e)
    {
        passwordGlobalVariable.var = passwordTextBox.Text;
    }

When I want to start a process I call it using this code (to run it as the username and password stored by the login form):

string fileName = "c:\\hdatools\\Ping2.exe";
string arguments = "";
string domain = "vantage";
private void Button2_Click(object sender, EventArgs e)
    {

    Process.Start(
    fileName,
    arguments,
    usernameGlobalVariable.var,
    passwordGlobalVariable.var,
    domain);

    }

The main error I get is on the line passwordGlobalVariable.var, The error says

Argument 4: cannot convert from 'string' to 'System.Security.SecureString'

I've tried different ways to try to convert `passwordGlobalVariable.var' to a secure string, or to give another string variable its contents and then render that variable as a secure string. I've finally run out of ideas. Thanks in advance for the help.

1
  • Why do you use a reserved keyword to name a variable? This is very confusing and misleading. An example to not follow. Commented Jun 18, 2014 at 21:07

3 Answers 3

3

Argument 4: cannot convert from 'string' to 'System.Security.SecureString'

because var is a string and not a SecureString, look:

class passwordGlobalVariable
{
   public static string var = "";
}

So change it to:

class passwordGlobalVariable
{
   public static SecureString var;
}

And later on, change your passwordTextBox_TextChanged event-handler to convert your password string into a SecureString:

private void passwordTextBox_TextChanged(object sender, EventArgs e)
{
   SecureString passWord = new SecureString();
   foreach (char c in passwordTextBox.Text.ToCharArray())
   {
     passWord.AppendChar(c);
   }

    passwordGlobalVariable.var = passWord;
}

A small side-note: Refrain from using the var word because one can be confused with the C#'s var contextual keyword.

Sign up to request clarification or add additional context in comments.

6 Comments

@Yair Nevet thanks! unfortunately it does not like my quotes. It underlines my "" and the error says Cannot implicitly convert type 'string to 'System.Security.SecureString'
@Yair Nevet thanks again--I changed that and it underlines passWord on the line passwordGlobalVariable.var = passWord; Error says **Argument 4: cannot convert from 'string' to 'System.Security.SecureString'
@superKing you should have no string fields in your data structures.
@YairNevet you are the man! That certainly did the trick and I hope others can benefit from this answer. I would vote it up if I had enough points. For some reason someone voted down my question. No idea why...anyway thanks again, bud! :D
@asadali it is not needed since var is a contextual keyword and not a completly reserved one.
|
2

The code below create a SecureString from the string var:

   SecureString passWord = new SecureString();
   foreach (char c in var.ToCharArray())
                  passWord.AppendChar(c);

2 Comments

thanks (and sorry as i'm new to coding) but will this make a new variable called passWord? Should I use the passWord variable to store the text in the password field? Will this variable be available globally? Thanks again
You can keep use the SecureString to store the passoword.
0
var secureUsername = new SecureString();
foreach (char c in usernameGlobalVariable.var)
{
    secureUsername.AppendChar(c);
}

var securePassword = new SecureString();
foreach (char c in passwordGlobalVariable.var)
{
    securePassword.AppendChar(c);
}

This will give you two SecureStrings that you can pass into the Process.Start method

2 Comments

thank you--I tried this, but it underlines var saying A namespace cannot directly contain members such as fields or methods It also underlines SecureString and says Expected class, delegate, enum, interface, or struct
Process.Start overloads accepts only one SecureString and it should be a password and not a username.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.