using System;
using System.Security;
public class Example
{
public static void Main()
{
char[] chars = { 't', 'e', 's', 't' };
// Instantiate the secure string.
SecureString testString = new SecureString();
// Assign the character array to the secure string.
foreach (char ch in chars)
testString.AppendChar(ch);
// Display secure string length.
Console.WriteLine("The length of the string is {0} characters.", testString.Length);
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
MakeReadOnly, RemoveAt
using System;
using System.Security;
using System.Diagnostics;
class MainClass {
public static SecureString ReadString() {
SecureString str = new SecureString();
ConsoleKeyInfo nextChar = Console.ReadKey(true);
while (nextChar.Key != ConsoleKey.Enter) {
if (nextChar.Key == ConsoleKey.Backspace) {
if (str.Length > 0) {
str.RemoveAt(str.Length - 1);
Console.Write(nextChar.KeyChar+" " +nextChar.KeyChar);
} else {
Console.Beep();
}
} else {
str.AppendChar(nextChar.KeyChar);
Console.Write("*");
}
nextChar = Console.ReadKey(true);
}
str.MakeReadOnly();
return str;
}
public static void Main() {
string user = "user1";
Console.Write("Enter the user's password: ");
using (SecureString pword = ReadString()) {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "notepad.exe";
startInfo.UserName = user;
startInfo.Password = pword;
startInfo.UseShellExecute = false;
using (Process process = new Process()) {
process.StartInfo = startInfo;
try {
process.Start();
} catch (Exception ex) {
Console.WriteLine("
Could not start Notepad process.");
Console.WriteLine(ex);
}
}
}
}
}
Demand
using System;
using Microsoft.Win32;
using System.Security.Permissions;
class Class1 {
static void Main(string[] args) {
RegistryPermission regPermission = new RegistryPermission(RegistryPermissionAccess.AllAccess, "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersion");
regPermission.Demand();
RegistryKey myRegKey = Registry.LocalMachine;
myRegKey = myRegKey.OpenSubKey("SOFTWAREMicrosoftWindows NTCurrentVersion");
Object oValue = myRegKey.GetValue("RegisteredOwner");
Console.WriteLine("OS Registered Owner: {0}", oValue.ToString());
}
}
Deny
using System;
using Microsoft.Win32;
using System.Security.Permissions;
class Class1 {
static void Main(string[] args) {
RegistryPermission regPermission = new RegistryPermission(RegistryPermissionAccess.AllAccess, "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersion");
regPermission.Deny();
RegistryKey myRegKey = Registry.LocalMachine;
myRegKey = myRegKey.OpenSubKey("SOFTWAREMicrosoftWindows NTCurrentVersion");
Object oValue = myRegKey.GetValue("RegisteredOwner");
Console.WriteLine("OS Registered Owner: {0}", oValue.ToString());
}
}
Demand PrincipalPermission
using System;
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;
class Class1 {
static void Main(string[] args) {
//AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity wi = WindowsIdentity.GetCurrent();
PrincipalPermission PrincipalPerm = new PrincipalPermission(wi.Name, "Administrator");
try {
PrincipalPerm.Demand();
Console.WriteLine("Code demand for an administrator succeeded.");
} catch (SecurityException e) {
Console.WriteLine("Demand for administrator failed!");
Console.WriteLine(e.Message);
}
}
}
Union two PrincipalPermissions
using System;
using System.Security.Permissions;
class MainClass {
public static void Method2() {
PrincipalPermission perm1 = new PrincipalPermission(null, @"MACHINEManagers");
PrincipalPermission perm2 = new PrincipalPermission(null, @"MACHINEDevelopers");
perm1.Union(perm2).Demand();
}
public static void Method3() {
PrincipalPermission perm = new PrincipalPermission(@"MACHINETester", @"MACHINEManagers");
perm.Demand();
}
[PrincipalPermission(SecurityAction.Demand, Name = @"MACHINETester")]
public static void Method4() {
}
[PrincipalPermission(SecurityAction.Demand, Role = @"MACHINEManagers")]
[PrincipalPermission(SecurityAction.Demand, Role = @"MACHINEDevelopers")]
public static void Method5() {
// Method implementation. . .
}
[PrincipalPermission(SecurityAction.Demand, Name = @"MACHINETester",
Role = @"MACHINEManagers")]
public static void Method6() {
// Method implementation. . .
}
}
new PrincipalPermission(null, @”MACHINEManagers”)
using System;
using System.Security.Permissions;
class MainClass {
public static void Method1() {
PrincipalPermission perm = new PrincipalPermission(@"MACHINETester", null);
perm.Demand();
}
}