Microsoft Virtualization Discussions

Data ONTAP Proxy SecureString issue?

rbarker
3,532 Views

So why is this C# code throwing Incorrect credentials when the same code logs me in via PowerShell.

PowerShell login using ConvertTo-SecureString:

Thanks,

Rob

1 ACCEPTED SOLUTION

rbarker
3,532 Views

Answered my own question but definitely with a head scratch. Modified code below; the only difference in the code is that I am reading the 3 parameters (controller IP/name, login and password) into string variables then using the resulting stored strings in my calls and everything works no problem.

       Proxy ontap = new Proxy();

            string IP = textBox1.Text.ToString();

            string login = textBox2.Text.ToString();

            string password = textBox3.Text.ToString();

            SecureString secpwd = new SecureString();

            foreach (char c in password)

       {

            secpwd.AppendChar(c);

       }

            PSCredential Credential = new PSCredential(login, secpwd);

           

       try

       {

                var controller = ontap.NaController.Connect(IP, null, Credential, commandSwitches:

                    NaController.ConnectAttributes.HTTPS |

                    NaController.ConnectAttributes.Transient).First();

                MessageBox.Show("Success", "NetApp Proxy Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);

       }

            catch (Exception ex)

       {

                MessageBox.Show("Error: " + ex.Message, "NetApp Proxy Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;

       }

Thanks,

Rob

View solution in original post

2 REPLIES 2

rbarker
3,532 Views

Just to be sure my PSCredential object is being set properly via SecureString I added the following code to unpack the SecureString and it is correct. The credentials are correct to log into the controller so not sure why my C# code is throwing this error. I call the below code with a MessageBox.Show(SecureStringToString(pwd)) and it shows the correct net@pp1.

static String SecureStringToString(SecureString value)

{

IntPtr bstr = Marshal.SecureStringToBSTR(value);

try

{

return Marshal.PtrToStringBSTR(bstr);

}

finally

{

               Marshal.FreeBSTR(bstr);

}

}

Thanks,

Rob

rbarker
3,533 Views

Answered my own question but definitely with a head scratch. Modified code below; the only difference in the code is that I am reading the 3 parameters (controller IP/name, login and password) into string variables then using the resulting stored strings in my calls and everything works no problem.

       Proxy ontap = new Proxy();

            string IP = textBox1.Text.ToString();

            string login = textBox2.Text.ToString();

            string password = textBox3.Text.ToString();

            SecureString secpwd = new SecureString();

            foreach (char c in password)

       {

            secpwd.AppendChar(c);

       }

            PSCredential Credential = new PSCredential(login, secpwd);

           

       try

       {

                var controller = ontap.NaController.Connect(IP, null, Credential, commandSwitches:

                    NaController.ConnectAttributes.HTTPS |

                    NaController.ConnectAttributes.Transient).First();

                MessageBox.Show("Success", "NetApp Proxy Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);

       }

            catch (Exception ex)

       {

                MessageBox.Show("Error: " + ex.Message, "NetApp Proxy Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;

       }

Thanks,

Rob

Public