本文整理汇总了C#中SecureString类的典型用法代码示例。如果您正苦于以下问题:C# SecureString类的具体用法?C# SecureString怎么用?C# SecureString使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SecureString类属于命名空间,在下文中一共展示了SecureString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateCredential
static SqlCredential CreateCredential(String username)
{
// Prompt the user for a password and construct SqlCredential
SecureString password = new SecureString();
Console.WriteLine("Enter password for " + username + ": ");
ConsoleKeyInfo nextKey = Console.ReadKey(true);
while (nextKey.Key != ConsoleKey.Enter)
{
if (nextKey.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.RemoveAt(password.Length - 1);
// erase the last * as well
Console.Write(nextKey.KeyChar);
Console.Write(" ");
Console.Write(nextKey.KeyChar);
}
}
else
{
password.AppendChar(nextKey.KeyChar);
Console.Write("*");
}
nextKey = Console.ReadKey(true);
}
Console.WriteLine();
Console.WriteLine();
password.MakeReadOnly();
return new SqlCredential(username, password);
}
示例2: GetPassword
/// <summary>
/// Read a password from the console into a SecureString
/// </summary>
/// <returns>Password stored in a secure string</returns>
public static SecureString GetPassword()
{
SecureString password = new SecureString();
Console.WriteLine("Enter password: ");
// get the first character of the password
ConsoleKeyInfo nextKey = Console.ReadKey(true);
while (nextKey.Key != ConsoleKey.Enter)
{
if (nextKey.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.RemoveAt(password.Length - 1);
// erase the last * as well
Console.Write(nextKey.KeyChar);
Console.Write(" ");
Console.Write(nextKey.KeyChar);
}
}
else
{
password.AppendChar(nextKey.KeyChar);
Console.Write("*");
}
nextKey = Console.ReadKey(true);
}
Console.WriteLine();
// lock the password down
password.MakeReadOnly();
return password;
}
示例3: getTokenString
public string getTokenString(string user, SecureString password)
{
//credentials.Add ( new System.Uri ( uri ), "Basic", new NetworkCredential ( boxUser.Text, boxPw.SecurePassword) );
//var url = Path.Combine ( URL, TOKEN );
var client = new HttpClient();
var content = new HttpRequestMessage(HttpMethod.Post, new Uri(URI, TOKEN));
content.Headers()
client.PostAsync(new Uri(URI, TOKEN),);
var request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URI, TOKEN));
request.ContentType = "text/json";
request.Credentials = new NetworkCredential(user, password);
request.Method = "POST";
//var objStream = request.GetRequestStream ();
//var writer = new StreamWriter ( objStream );
//writer.Write ( "{\"comment\":\"ProtonetTool\"}" );
//writer.Flush ();
//writer.Close ();
return ReadResponse(request);
}
示例4: ToSecureString
/// <summary>
/// A String extension method that converts the @this to a secure string.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>@this as a SecureString.</returns>
public static SecureString ToSecureString(this string @this)
{
var secureString = new SecureString();
foreach (Char c in @this)
secureString.AppendChar(c);
return secureString;
}
示例5: createSecureString
private static SecureString createSecureString(string s)
{
SecureString result = new SecureString();
foreach(char ch in s)
{
result.AppendChar(ch);
}
return result;
}
示例6: ToSecureString
public static SecureString ToSecureString(string input)
{
SecureString secure = new SecureString();
foreach (char c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
示例7: ReadPassword
public static SecureString ReadPassword()
{
Console.Write("Password: ");
SecureString secPass = new SecureString();
ConsoleKeyInfo key = Console.ReadKey(true);
while (key.KeyChar != '\r')
{
secPass.AppendChar(key.KeyChar);
key = Console.ReadKey(true);
}
return secPass;
}
示例8: X509Certificate
public X509Certificate(byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
{
if (rawData == null || rawData.Length == 0)
throw new ArgumentException(SR.Arg_EmptyOrNullArray, nameof(rawData));
ValidateKeyStorageFlags(keyStorageFlags);
using (var safePasswordHandle = new SafePasswordHandle(password))
{
Pal = CertificatePal.FromBlob(rawData, safePasswordHandle, keyStorageFlags);
}
}
示例9: ToInsecureString
public static string ToInsecureString(SecureString input)
{
string returnValue = string.Empty;
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
try
{
returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
}
finally
{
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
}
return returnValue;
}
示例10: ToSecureString
public static SecureString ToSecureString(this IEnumerable<char> input)
{
if (input == null)
{
return null;
}
var secure = new SecureString();
foreach (var c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
示例11: VerifyString
private static void VerifyString(SecureString ss, string exString)
{
IntPtr uniStr = IntPtr.Zero;
try
{
uniStr = SecureStringMarshal.SecureStringToCoTaskMemUnicode(ss);
string acString = Marshal.PtrToStringUni(uniStr);
Assert.Equal(exString.Length, acString.Length);
Assert.Equal(exString, acString);
}
finally
{
if (uniStr != IntPtr.Zero)
SecureStringMarshal.ZeroFreeCoTaskMemUnicode(uniStr);
}
}
示例12: CreateSecureString
private static SecureString CreateSecureString(string exValue)
{
SecureString ss = null;
if (string.IsNullOrEmpty(exValue))
ss = new SecureString();
else
{
unsafe
{
fixed (char* mychars = exValue.ToCharArray())
ss = new SecureString(mychars, exValue.Length);
}
}
Assert.NotNull(ss);
return ss;
}
示例13: GetSshKey
public static ISshKey GetSshKey(this EntrySettings settings,
ProtectedStringDictionary strings, ProtectedBinaryDictionary binaries,
SprContext sprContext)
{
if (!settings.AllowUseOfSshKey) {
return null;
}
KeyFormatter.GetPassphraseCallback getPassphraseCallback =
delegate(string comment)
{
var securePassphrase = new SecureString();
var passphrase = SprEngine.Compile(strings.ReadSafe(
PwDefs.PasswordField), sprContext);
foreach (var c in passphrase) {
securePassphrase.AppendChar(c);
}
return securePassphrase;
};
Func<Stream> getPrivateKeyStream;
Func<Stream> getPublicKeyStream = null;
switch (settings.Location.SelectedType) {
case EntrySettings.LocationType.Attachment:
if (string.IsNullOrWhiteSpace(settings.Location.AttachmentName)) {
throw new NoAttachmentException();
}
var privateKeyData = binaries.Get(settings.Location.AttachmentName);
var publicKeyData = binaries.Get(settings.Location.AttachmentName + ".pub");
getPrivateKeyStream = () => new MemoryStream(privateKeyData.ReadData());
if (publicKeyData != null)
getPublicKeyStream = () => new MemoryStream(publicKeyData.ReadData());
return GetSshKey(getPrivateKeyStream, getPublicKeyStream,
settings.Location.AttachmentName, getPassphraseCallback);
case EntrySettings.LocationType.File:
getPrivateKeyStream = () => File.OpenRead(settings.Location.FileName);
var publicKeyFile = settings.Location.FileName + ".pub";
if (File.Exists(publicKeyFile))
getPublicKeyStream = () => File.OpenRead(publicKeyFile);
return GetSshKey(getPrivateKeyStream, getPublicKeyStream,
settings.Location.AttachmentName, getPassphraseCallback);
default:
return null;
}
}
示例14: SecureStringToCoTaskMemAnsi
public static IntPtr SecureStringToCoTaskMemAnsi(SecureString s) => s != null ? s.MarshalToString(globalAlloc: false, unicode: false) : IntPtr.Zero;
示例15: SecureStringToGlobalAllocUnicode
public static IntPtr SecureStringToGlobalAllocUnicode(SecureString s) => s != null ? s.MarshalToString(globalAlloc: true, unicode: true) : IntPtr.Zero;