C# WPF中 SecureString 转 String

这段代码是用于WPF控件中的PasswordBox的SecureString解密为明文

这个地方的代码我想起来了,来自Youtuber:AngleSix,各位嘤语好可以去康康他的Youtube视频,之前我在初学WPF的时候就看他视频,受益匪浅~

 1 public static string UnSecure(this SecureString secureString)
 2         {
 3             //Make sure we have a secure string 
 4             if (secureString == null)
 5             {
 6                 return string.Empty;
 7             }
 8             //Get a pointer for an unsecure string in memory
 9             var unmanagedString = IntPtr.Zero;
10             try
11             {
12                 //Unsecures the password
13                 unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
14                 return Marshal.PtrToStringUni(unmanagedString);
15             }
16             finally
17             {
18                 //Clean up any memory allocation
19                 Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
20             }
21         }
原文地址:https://www.cnblogs.com/MichaelJson/p/12894656.html