本文整理汇总了C#中System.Security.SecureString.RemoveAt方法的典型用法代码示例。如果您正苦于以下问题:C# SecureString.RemoveAt方法的具体用法?C# SecureString.RemoveAt怎么用?C# SecureString.RemoveAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.SecureString
的用法示例。
在下文中一共展示了SecureString.RemoveAt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSecureStringFromConsole
public static SecureString GetSecureStringFromConsole()
{
var password = new SecureString();
Console.Write("Enter Password: ");
while (true)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter) break;
if (cki.Key == ConsoleKey.Escape)
{
password.Dispose();
return null;
}
if (cki.Key == ConsoleKey.Backspace)
{
if (password.Length != 0)
password.RemoveAt(password.Length - 1);
}
else password.AppendChar(cki.KeyChar);
}
return password;
}
示例2: ReadPassword
/// <summary>
/// Reads a password from the user in a safe fashion.
/// </summary>
/// <param name="prompt">A string, shown to the user as prompt.</param>
/// <returns>An instance of <see cref="SecureString"/>.</returns>
public SecureString ReadPassword(string prompt)
{
Console.WriteLine(prompt);
// http://stackoverflow.com/questions/3404421/password-masking-console-application
Console.Write("Password: ");
SecureString pwd = new SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd.RemoveAt(pwd.Length - 1);
//Console.Write("\b \b");
}
}
else
{
pwd.AppendChar(i.KeyChar);
//Console.Write("*");
}
}
Console.WriteLine();
return pwd;
}
示例3: takePassword
private SecureString takePassword ()
{
SecureString result = new SecureString();
Console.Write("Password: ");
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (result.Length > 0)
{
result.RemoveAt(result.Length - 1);
Console.Write("\b \b");
}
}
else if (i.Key == ConsoleKey.Spacebar || result.Length > 20) {}
else
{
result.AppendChar(i.KeyChar);
Console.Write("*");
}
}
Console.WriteLine();
return result;
}
示例4: GetPassword
/// <summary>
/// Get a password from the console
/// </summary>
/// <returns>Password stored in a SecureString</returns>
private static SecureString GetPassword()
{
var password = new SecureString();
while (true)
{
var keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Enter)
{
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.RemoveAt(password.Length - 1);
Console.Write("\b \b");
}
}
else
{
password.AppendChar(keyInfo.KeyChar);
Console.Write("*");
}
}
return password;
}
示例5: ReadConsoleByKeySecure
public static SecureString ReadConsoleByKeySecure(bool hideCharacters = false)
{
var running = true;
var finalString = new SecureString();
while (running)
{
var letter = Console.ReadKey(hideCharacters).KeyChar;
if (letter == (char)13)
running = false;
else if (letter == (char)8 && finalString.Length >= 1)
{
finalString.RemoveAt(finalString.Length - 1);
Console.CursorLeft--;
Console.Write('\0');
Console.CursorLeft--;
}
else if (letter != (char)8)
{
finalString.AppendChar(letter);
if (hideCharacters)
Console.Write('*');
}
}
Console.WriteLine();
return finalString;
}
示例6: UnsafeConstructor
public unsafe void UnsafeConstructor ()
{
try {
SecureString ss = null;
char[] data = new char[] { 'a', 'b', 'c' };
fixed (char* p = &data[0]) {
ss = new SecureString (p, data.Length);
}
Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
Assert.AreEqual (3, ss.Length, "3");
ss.AppendChar ('a');
Assert.AreEqual (4, ss.Length, "4");
ss.Clear ();
Assert.AreEqual (0, ss.Length, "0b");
ss.InsertAt (0, 'b');
Assert.AreEqual (1, ss.Length, "1b");
ss.SetAt (0, 'c');
Assert.AreEqual (1, ss.Length, "1c");
ss.RemoveAt (0);
Assert.AreEqual (0, ss.Length, "0c");
ss.Dispose ();
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
示例7: EnterPassword
public static SecureString EnterPassword()
{
SecureString pwd = new SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd.RemoveAt(pwd.Length - 1);
Console.Write("\b \b");
}
}
else
{
pwd.AppendChar(i.KeyChar);
Console.Write("*");
}
}
return pwd;
}
示例8: ReadPasswordFromConsole
/// <summary>
/// Read a password from the console and return it as SecureString
/// </summary>
/// <returns></returns>
public static bool ReadPasswordFromConsole(out SecureString secStr)
{
secStr = new SecureString();
for (ConsoleKeyInfo c = Console.ReadKey(true); c.Key != ConsoleKey.Enter; c = Console.ReadKey(true))
{
if (c.Key == ConsoleKey.Backspace && secStr.Length > 0)
secStr.RemoveAt(secStr.Length - 1);
if (c.Key == ConsoleKey.Escape)
{
// cancel
secStr.Dispose();
Console.WriteLine();
return false;
}
if (!Char.IsControl(c.KeyChar))
secStr.AppendChar(c.KeyChar);
}
secStr.MakeReadOnly();
Console.WriteLine();
return true;
}
示例9: ReadSecureString
public static SecureString ReadSecureString(string prompt)
{
const string t = " !\"#$%&'()*+,-./0123456789:;<=>[email protected][\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
SecureString securePwd = new SecureString();
ConsoleKeyInfo key;
Console.Write(prompt);
Console.Write(':');
do
{
key = Console.ReadKey(true);
if (t.IndexOf(key.KeyChar) != -1)
{
securePwd.AppendChar(key.KeyChar);
Console.Write('*');
}
else if (key.Key == ConsoleKey.Backspace && securePwd.Length > 0)
{
securePwd.RemoveAt(securePwd.Length - 1);
Console.Write(key.KeyChar);
Console.Write(' ');
Console.Write(key.KeyChar);
}
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
securePwd.MakeReadOnly();
return securePwd;
}
示例10: SecureStringUsingExample
// secure string for passwords or secret string data
public static void SecureStringUsingExample()
{
using (SecureString securePassword = new SecureString())
{
// getting
while (true)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (cki.Key == ConsoleKey.Backspace)
{
if (securePassword.Length > 0)
{
securePassword.RemoveAt(securePassword.Length - 1);
Console.Write("-");
}
continue;
}
securePassword.AppendChar(cki.KeyChar);
Console.Write("*");
}
// showing
unsafe
{
char* buffer = null;
try
{
// decrypting
buffer = (char*)Marshal.SecureStringToCoTaskMemUnicode(securePassword);
for (var i = 0; buffer[i] != 0; i++)
Console.Write(buffer[i]);
}
finally
{
// buffer clearing
if (buffer != null) Marshal.ZeroFreeCoTaskMemUnicode((IntPtr)buffer);
}
}
securePassword.Dispose(); // not necessary in using construction
}
}
示例11: GetPasswordFromConsole
private static SecureString GetPasswordFromConsole()
{
SecureString password = new SecureString();
bool readingPassword = true;
Console.Write("Enter password: ");
while (readingPassword)
{
ConsoleKeyInfo userInput = Console.ReadKey(true);
switch (userInput.Key)
{
case (ConsoleKey.Enter):
readingPassword = false;
break;
case (ConsoleKey.Escape):
password.Clear();
readingPassword = false;
break;
case (ConsoleKey.Backspace):
if (password.Length > 0)
{
password.RemoveAt(password.Length - 1);
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
break;
default:
if (userInput.KeyChar != 0)
{
password.AppendChar(userInput.KeyChar);
Console.Write("*");
}
break;
}
}
Console.WriteLine();
password.MakeReadOnly();
return password;
}
示例12: ReadLineAsSecureString
public static SecureString ReadLineAsSecureString()
{
var secureString = new SecureString();
try
{
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
if (keyInfo.Key == ConsoleKey.Backspace)
{
if (secureString.Length < 1)
{
continue;
}
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(' ');
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
secureString.RemoveAt(secureString.Length - 1);
}
else
{
secureString.AppendChar(keyInfo.KeyChar);
Console.Write('*');
}
}
Console.WriteLine(String.Empty);
}
catch (InvalidOperationException)
{
// This can happen when you redirect nuget.exe input, either from the shell with "<" or
// from code with ProcessStartInfo.
// In this case, just read data from Console.ReadLine()
foreach (var c in Console.ReadLine())
{
secureString.AppendChar(c);
}
}
secureString.MakeReadOnly();
return secureString;
}
示例13: ReadKey
private bool ReadKey(SecureString buffer)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
return true;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (buffer.Length > 0)
{
buffer.RemoveAt(buffer.Length - 1);
Console.Write("\b \b");
}
}
else
{
buffer.AppendChar(i.KeyChar);
Console.Write("*");
}
return false;
}
示例14: DefaultConstructor
public void DefaultConstructor ()
{
try {
SecureString ss = new SecureString ();
Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
Assert.AreEqual (0, ss.Length, "0");
ss.AppendChar ('a');
Assert.AreEqual (1, ss.Length, "1");
ss.Clear ();
Assert.AreEqual (0, ss.Length, "0b");
ss.InsertAt (0, 'b');
Assert.AreEqual (1, ss.Length, "1b");
ss.SetAt (0, 'c');
Assert.AreEqual (1, ss.Length, "1c");
Assert.AreEqual ("System.Security.SecureString", ss.ToString (), "ToString");
ss.RemoveAt (0);
Assert.AreEqual (0, ss.Length, "0c");
ss.Dispose ();
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
示例15: GetPassword
/// <summary>
/// Helper to return the password
/// </summary>
/// <returns>SecureString representing the password</returns>
public static SecureString GetPassword()
{
SecureString sStrPwd = new SecureString();
try
{
Console.Write("SharePoint Password: ");
for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
{
if (keyInfo.Key == ConsoleKey.Backspace)
{
if (sStrPwd.Length > 0)
{
sStrPwd.RemoveAt(sStrPwd.Length - 1);
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
}
else if (keyInfo.Key != ConsoleKey.Enter)
{
Console.Write("*");
sStrPwd.AppendChar(keyInfo.KeyChar);
}
}
Console.WriteLine("");
}
catch (Exception e)
{
sStrPwd = null;
Console.WriteLine(e.Message);
}
return sStrPwd;
}