本文整理汇总了C#中SecureString.AppendChar方法的典型用法代码示例。如果您正苦于以下问题:C# SecureString.AppendChar方法的具体用法?C# SecureString.AppendChar怎么用?C# SecureString.AppendChar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SecureString
的用法示例。
在下文中一共展示了SecureString.AppendChar方法的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: 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;
}
示例4: createSecureString
private static SecureString createSecureString(string s)
{
SecureString result = new SecureString();
foreach(char ch in s)
{
result.AppendChar(ch);
}
return result;
}
示例5: ToSecureString
public static SecureString ToSecureString(string input)
{
SecureString secure = new SecureString();
foreach (char c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
示例6: 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;
}
示例7: 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;
}
示例8: Go
public static void Go() {
using (SecureString ss = new SecureString()) {
Console.Write("Please enter password: ");
while (true) {
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter) break;
// Append password characters into the SecureString
ss.AppendChar(cki.KeyChar);
Console.Write("*");
}
Console.WriteLine();
// Password entered, display it for demonstrattion purposes
DisplaySecureString(ss);
}
// After 'using', the SecureString is Disposed; no sensitive data in memory
}
示例9: 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;
}
}
示例10: FindKeePassPassword
public static SecureString FindKeePassPassword(
PwDatabase database,
bool caseSensitive = false,
string username = null,
string url = null,
string title = null,
string notes = null)
{
var entries = FindKeePassEntries(database, caseSensitive, true, username, url, title, notes);
if (entries == null)
return null;
foreach(var entry in entries)
{
var secureString = new SecureString();
var value = entry.Strings.ReadSafe("Password");
foreach (var c in value)
secureString.AppendChar(c);
return secureString;
}
return null;
}
示例11: AppendChar
private static SecureString AppendChar(SecureString ss, char c)
{
ss.AppendChar(c);
return ss;
}
示例12: ThreadSafe_Stress
public static void ThreadSafe_Stress(int executionTimeSeconds)
{
using (var ss = new SecureString())
{
DateTimeOffset end = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(executionTimeSeconds);
Task.WaitAll(Enumerable.Range(0, Environment.ProcessorCount).Select(_ => Task.Run(() =>
{
var rand = new Random(Task.CurrentId.Value);
while (DateTimeOffset.UtcNow < end)
{
char c = (char)rand.Next(0, char.MaxValue);
switch (rand.Next(12))
{
case 0:
ss.AppendChar(c);
break;
case 1:
ss.InsertAt(0, c);
break;
case 2:
try { ss.SetAt(0, c); } catch (ArgumentOutOfRangeException) { }
break;
case 3:
ss.Copy().Dispose();
break;
case 4:
Assert.InRange(ss.Length, 0, ushort.MaxValue + 1);
break;
case 5:
ss.Clear();
break;
case 6:
try { ss.RemoveAt(0); } catch (ArgumentOutOfRangeException) { }
break;
case 7:
Assert.False(ss.IsReadOnly());
break;
case 8:
Marshal.ZeroFreeCoTaskMemAnsi(SecureStringMarshal.SecureStringToCoTaskMemAnsi(ss));
break;
case 9:
Marshal.ZeroFreeCoTaskMemUnicode(SecureStringMarshal.SecureStringToCoTaskMemUnicode(ss));
break;
case 10:
Marshal.ZeroFreeGlobalAllocAnsi(SecureStringMarshal.SecureStringToGlobalAllocAnsi(ss));
break;
case 11:
Marshal.ZeroFreeGlobalAllocUnicode(SecureStringMarshal.SecureStringToGlobalAllocUnicode(ss));
break;
}
}
})).ToArray());
}
}
示例13: createEventQuery
private EventLogReader createEventQuery(EmployeeProfile emp)
{
DateTime now = DateTime.Now;
DateTime endDate = now;
DateTime startDate = DateTime.Today.AddDays(AppConfig.DisplayDays * -1);
Session["DateTime"] = startDate;
string queryString = String.Format(AppConfig.EventQuery,
startDate.ToUniversalTime().ToString("o"),
endDate.ToUniversalTime().ToString("o"));
EventLogQuery query = new EventLogQuery("System", PathType.LogName, queryString);
if (emp.Machine.CompareTo("127.0.0.1") != 0 && AppConfig.RemoteQuery) {
// Query the Application log on the remote computer.
SecureString pw = new SecureString();
string passwd = emp.Password;
if (!string.IsNullOrEmpty(passwd)) {
char[] charArray = passwd.ToCharArray();
for (int i = 0; i < passwd.Length; i++) { pw.AppendChar(charArray[i]); }
}
pw.MakeReadOnly();
query.Session = new EventLogSession(
emp.Machine, // Remote Computer
emp.Domain, // Domain
emp.Name, // Username
pw,
SessionAuthentication.Default);
//pw.Dispose();
Session["SecureString"] = pw;
}
Session["EventLogQuery"] = queryString;
return new EventLogReader(query);
}
示例14: CreatePfxDataPasswordSecureString
public static SecureString CreatePfxDataPasswordSecureString()
{
var s = new SecureString();
// WARNING:
// A key value of SecureString is in keeping string data off of the GC heap, such that it can
// be reliably cleared when no longer needed. Creating a SecureString from a string or converting
// a SecureString to a string diminishes that value. These conversion functions are for testing that
// SecureString works, and does not represent a pattern to follow in any non-test situation.
foreach (char c in PfxDataPassword.ToCharArray())
{
s.AppendChar(c);
}
return s;
}
示例15: ConvertToSecureString
private SecureString ConvertToSecureString(string password)
{
if (password == null)
throw new ArgumentNullException("password");
var securePassword = new SecureString();
foreach (char c in password)
securePassword.AppendChar(c);
securePassword.MakeReadOnly();
return securePassword;
}