title: C# - compare two SecureStrings for equality
body: <p>I have a WPF application with two PasswordBoxes, one for the password and another for the password to be entered a second time for confirmation purposes.  I was wanting to use <code>PasswordBox.SecurePassword</code> to get the <code>SecureString</code> of the password, but I need to be able to compare the contents of the two PasswordBoxes to ensure equality before I accept the password.  However, two identical SecureStrings are not considered equal:</p>
  
  <pre><code>var secString1 = new SecureString();
  var secString2 = new SecureString();
  foreach (char c in "testing")
  {
      secString1.AppendChar(c);
      secString2.AppendChar(c);
  }
  Assert.AreEqual(secString1, secString2); // This fails
  </code></pre>
  
  <p>I was thinking comparing the <code>Password</code> property of the PasswordBoxes would defeat the point of accessing only <code>SecurePassword</code> because I'd be reading the plain-text password.  What should I do to compare the two passwords without sacrificing <a href="https://stackoverflow.com/questions/141203/when-would-i-need-a-securestring-in-net">security</a>?</p>
  
  <p><strong>Edit</strong>:  based on <a href="https://stackoverflow.com/questions/978350">this question</a>, I'm checking out <a href="http://bartdesmet.net/blogs/bart/archive/2006/03/31/3851.aspx" rel="noreferrer">this blog post</a> about "using the Marshal class to convert the SecureString to ANSI or Unicode or a BSTR", then maybe I can compare those.</p>