<%@ PAGE LANGUAGE="C#" %>
<%@ Import Namespace="Microsoft.Win32" %>
<html>
<head>
<title>get username</title>
<style>
table{border-collapse:collapse;}
tr{vertical-align:top;}
td{border:1px solid #999;
font-family:monospace;}
</style>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
String str_result = "";
String domain_key = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
String domain_val = "Domain";
String dn_key = "SYSTEM\\CurrentControlSet\\Services\\NTDS\\Parameters";
String dn_val = "Root Domain";
String no_key = "no key";
String no_val = "no val";
str_result = "<tr><td>正常アクセス "+domain_val+"</td><td>";
try
{
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(domain_key)){
str_result += (String)(rk.GetValue(domain_val,"(not found)"));
rk.Close();
}
}
catch(System.Security.SecurityException se1)
{
str_result += se1.Message;
}
catch(NullReferenceException nr1)
{
str_result += nr1.Message;
}
str_result += "</td></tr>";
str_result += "<tr><td>アクセス拒否 "+dn_val+"</td><td>";
try
{
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(dn_key)){
str_result += (String)(rk.GetValue(dn_val,"(not found)"));
rk.Close();
}
}
catch(System.Security.SecurityException se2) // ここを通過する
{
str_result += se2.Message;
}
catch(NullReferenceException nr2)
{
str_result += nr2.Message;
}
str_result += "</td></tr>";
str_result += "<tr><td>キーなし "+no_key+"</td><td>";
try
{
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(no_key)){
str_result += (String)(rk.GetValue(no_val,"(not found)"));
rk.Close();
}
}
catch(System.Security.SecurityException se3)
{
str_result += se3.Message;
}
catch(NullReferenceException nr3) // ここを通過する
{
str_result += nr3.Message;
}
str_result += "</td></tr>";
str_result += "<tr><td>値なし "+no_val+"</td><td>";
try
{
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(domain_key)){
str_result += (String)(rk.GetValue(no_val,"(not found)"));
rk.Close();
}
}
catch(System.Security.SecurityException se4)
{
str_result += se4.Message;
}
catch(NullReferenceException nr4)
{
str_result += nr4.Message;
}
str_result += "</td></tr>";
lbl_result.Text = "<table>"+str_result+"</table>";
}
</script>
</head>
<body>
<form runat="server">
<asp:Label id="lbl_result" Text="" runat="server" />
</form>
</body>
</html>
作成 2011.02.12
更新 2011.02.13
更新 2011.02.13
ASP.NET でレジストリ参照
コード
正常アクセス、セキュリティエラー、キーなし、値なしの4種類を試しています。
実行結果
| 正常アクセス Domain | test.lan |
| アクセス拒否 Root Domain | 要求されたレジストリ アクセスは許可されていません。 |
| キーなし no key | オブジェクト参照がオブジェクト インスタンスに設定されていません。 |
| 値なし no val | (not found) |