本文整理汇总了C#中SecureString.RemoveAt方法的典型用法代码示例。如果您正苦于以下问题:C# SecureString.RemoveAt方法的具体用法?C# SecureString.RemoveAt怎么用?C# SecureString.RemoveAt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SecureString
的用法示例。
在下文中一共展示了SecureString.RemoveAt方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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());
}
}
示例4: ReadSecureString
/// <summary>
/// Read a SecureString from the Console
/// </summary>
/// <param name="passwordChar">char to display instead of real char. (char)0 : nothing, null : inputchar</param>
/// <returns>ReadOnly SecureString</returns>
public static SecureString ReadSecureString(char? passwordChar = default(char?))
{
var secureStr = new SecureString();
while (true)
{
var keyInfo = Console.ReadKey(true);
switch (keyInfo.Key)
{
case ConsoleKey.Backspace:
if (secureStr.Length > 0)
{
secureStr.RemoveAt(secureStr.Length - 1);
if (passwordChar != null) Console.Write("\b \b");
}
break;
case ConsoleKey.Enter:
goto end;
case ConsoleKey.A:
case ConsoleKey.B:
case ConsoleKey.C:
case ConsoleKey.D:
case ConsoleKey.E:
case ConsoleKey.F:
case ConsoleKey.G:
case ConsoleKey.H:
case ConsoleKey.I:
case ConsoleKey.J:
case ConsoleKey.K:
case ConsoleKey.L:
case ConsoleKey.M:
case ConsoleKey.N:
case ConsoleKey.O:
case ConsoleKey.P:
case ConsoleKey.Q:
case ConsoleKey.R:
case ConsoleKey.S:
case ConsoleKey.T:
case ConsoleKey.U:
case ConsoleKey.V:
case ConsoleKey.W:
case ConsoleKey.X:
case ConsoleKey.Y:
case ConsoleKey.Z:
case ConsoleKey.NumPad0:
case ConsoleKey.NumPad1:
case ConsoleKey.NumPad2:
case ConsoleKey.NumPad3:
case ConsoleKey.NumPad4:
case ConsoleKey.NumPad5:
case ConsoleKey.NumPad6:
case ConsoleKey.NumPad7:
case ConsoleKey.NumPad8:
case ConsoleKey.NumPad9:
case ConsoleKey.Add:
case ConsoleKey.Subtract:
case ConsoleKey.Multiply:
case ConsoleKey.Divide:
case ConsoleKey.Oem1:
secureStr.AppendChar(keyInfo.KeyChar);
Console.Write(passwordChar ?? keyInfo.KeyChar);
break;
}
}
end:
Console.WriteLine();
secureStr.MakeReadOnly();
return secureStr;
}
示例5: GetSecPswd
private static SecureString GetSecPswd(String prompt) {
SecureString password = new SecureString();
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(prompt);
Console.ForegroundColor = ConsoleColor.Magenta;
while (true) {
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter) {
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
return password;
}
else if (cki.Key == ConsoleKey.Backspace) {
// remove the last asterisk from the screen...
if (password.Length > 0) {
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
password.RemoveAt(password.Length - 1);
}
}
else if (cki.Key == ConsoleKey.Escape) {
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
return password;
}
else if (Char.IsLetterOrDigit(cki.KeyChar) || Char.IsSymbol(cki.KeyChar)) {
if (password.Length < 20) {
password.AppendChar(cki.KeyChar);
Console.Write("*");
}
else {
Console.Beep();
}
}
else {
Console.Beep();
}
}
}