2012-03-16 3 views
2

암호 상자가있어서 확인을 위해 입력 데이터를 얻고 싶습니다.변수에 대한 passwordbox의 값 사용

내 PasswordBox의의 C# 코드

public void textBox2_TextInput(object sender, TextCompositionEventArgs e) 
    { 
     //pass = textBox2.ToString(); 
    } 

와 XAML 코드는

<PasswordBox Name="textBox2" 
      PasswordChar="*" 
      TextInput="textBox2_TextInput" /> 

이 내가 암호

private void loginbutton_Click(object sender, RoutedEventArgs e) 
    { 
     usr = textBox1.Text; 
     SecureString passdat =textBox2.SecurePassword; 
     pass = passdat.ToString(); 
    }    

이 null.This가있다 반환을 캡처 작성한 것입니다 더미 데모 그래서 암호화가 필요하지 않습니다. 이전에 텍스트 상자를 사용하고 확인 작업. 암호를 사용하여 b 황소는 단지 복잡한 것들.

답변

2

SecureString class 값을 볼 수 없습니다. 그것이 그 핵심입니다. SecureString는, 비교, 또는 값을 변환 검사 회원이 없습니다 것을

private void loginbutton_Click(object sender, RoutedEventArgs e) 
{ 
    usr = textBox1.Text; 
    String pass = textBox2.Password; 
} 
2

참고 : PasswordBox의에 입력 한 값으로 작동 할 수있게하려면, PasswordBox의 대신 SecurePassword 회원의 비밀번호 멤버를 사용 SecureString. 그러한 회원이 없으면 우발적 인 또는 악의적 인 노출로부터 인시던트의 가치를 보호하는 데 도움이됩니다. SecureStringToBSTR 메서드와 같은 System.Runtime.InteropServices.Marshal 클래스의 해당 멤버를 사용하여 SecureString 개체의 값을 조작합니다.

 private void loginbutton_Click(object sender, RoutedEventArgs e) 
     { 
      usr = textBox1.Text; 
      txtPassword=textBox2.Text; 

      SecureString objSecureString=new SecureString(); 
      char[] passwordChar = txtPassword.ToCharArray(); 
      foreach (char c in passwordChar) 
        objSecureString.AppendChar(c); 
      objSecureString.MakeReadOnly();//Notice at the end that the MakeReadOnly command prevents the SecureString to be edited any further. 


      //Reading a SecureString is more complicated. There is no simple ToString method, which is also intended to keep the data secure. To read the data C# developers must access the data in memory directly. Luckily the .NET Framework makes it fairly simple: 
      IntPtr stringPointer = Marshal.SecureStringToBSTR(objSecureString); 
      string normalString = Marshal.PtrToStringBSTR(stringPointer);//Original Password text 

     } 
+0

. 매우 유용한 감사 –