<%@ Page Language="C#" EnableViewStateMac="true" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<%@ Import Namespace="Microsoft.Win32" %>
<!DOCTYPE html>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<dl>");
sb.AppendFormat("<dt>Request Url</dt><dd>{0}</dd>",
Request.Url);
sb.AppendFormat("<dt>DomainAppVirtualPath</dt><dd>{0}</dd>",
HttpRuntime.AppDomainAppVirtualPath);
//Display ASP.NET process identity information
WindowsIdentity wid = WindowsIdentity.GetCurrent();
sb.AppendFormat("<dt>AppPool Identity</dt><dd>{0}({1})</dd>",
wid.Name, wid.User.Value);
//Get current ASP.NET version
Version version = System.Environment.Version;
string aspNetVer = string.Format("{0}.{1}.{2}.0",
version.Major, version.Minor, version.Build);
//Check HKCU
string regPath = string.Format(@"Software\Microsoft\ASP.NET\{0}",
aspNetVer);
RegistryKey key = Registry.CurrentUser.OpenSubKey(regPath);
sb.AppendFormat("<dt>Registry</dt><dd>[HKEY_CURRENT_USER\\{0}]</dd>",
regPath);
//Check HKLM
regPath = string.Format(@"Software\Microsoft\ASP.NET\{0}\AutoGenKeys\{1}",
aspNetVer, wid.User.Value);
key = Registry.LocalMachine.OpenSubKey(regPath);
sb.AppendFormat("<dt>Registry</dt><dd>[HKEY_LOCAL_MACHINE\\{0}]</dd>",
regPath);
//Get machineKey settings
MachineKeySection mks = (MachineKeySection)
ConfigurationManager.GetSection("system.web/machineKey");
sb.AppendFormat("<dt>DecryptionKey Setting</dt><dd>{0}</dd>",
mks.DecryptionKey);
Type mksType = typeof(MachineKeySection);
//After using machine key to encrypt, DecryptionKeyInternal will be cleared
//Uncomment below line, you will always get 00-00-00-00
//FormsAuthentication.Encrypt(new FormsAuthenticationTicket("Jeffre", false, 60));
sb.AppendFormat("<dt>DecryptionKeyInternal</dt><dd>{0}</dd>",
BitConverter.ToString(
(byte[]) mksType.GetProperty("DecryptionKeyInternal",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(mks, null)));
SymmetricAlgorithm sa =
(SymmetricAlgorithm)mksType.GetField("s_oSymAlgoDecryption",
BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
string result = "null";
if (sa != null) result = BitConverter.ToString(sa.Key);
sb.AppendFormat("<dt>s_oSymAlgoDecryption.Key</dt><dd>{0}</dd>",
result);
//HttpRuntime.s_autogenKeys
byte[] rtAutoGenKeys = (byte[])typeof(HttpRuntime)
.GetField("s_autogenKeys", BindingFlags.NonPublic | BindingFlags.Static)
.GetValue(null);
sb.AppendFormat("<dt>HttpRuntime.s_autogenKeys</dt><dd>{0}</dd>",
BitConverter.ToString(rtAutoGenKeys));
sb.Append("</dl>");
disp.InnerHtml = sb.ToString();
}
</script>
<html>
<head runat="server">
<title>AutoGen MachineKey Test</title>
</head>
<body>
<form id="form1" runat="server">
<div id="disp" runat="server" enableviewstate="false"></div>
<asp:Button ID="bPostBack" runat="server" Text="Postback"/>
</form>
</body>
</html>