SecurityAction を指定して、 RegistryPermissionAttribute クラスの新しいインスタンスを初期化します。
Public Sub New( _
ByVal action As SecurityAction _)
[C#]
public RegistryPermissionAttribute(
SecurityActionaction);
[C++]
public: RegistryPermissionAttribute(
SecurityActionaction);
[JScript]
public function RegistryPermissionAttribute(
action : SecurityAction);
パラメータ
- action
SecurityAction 値の 1 つ。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | action パラメータが、有効な SecurityAction ではありません。 |
使用例
' This sample demonstrates the use of the RegistryPermissionAttribute.
Imports System
Imports System.Reflection
Imports System.Security.Permissions
Imports System.Security
Imports System.IO
Imports Microsoft.VisualBasic
Class [MyClass]
Public Shared Sub RegistryPermissionAttributeDemo()
Try
PermitOnlyMethod()
Catch e As Exception
Console.WriteLine(e.Message.ToString())
End Try
Try
DenyMethod()
Catch e As Exception
Console.WriteLine(e.Message.ToString())
End Try
Try
DenyAllMethod()
Catch e As Exception
Console.WriteLine(e.Message.ToString())
End Try
End Sub 'RegistryPermissionAttributeDemo
' This method demonstrates the use of the RegistryPermissionAttribute to create PermitOnly permissions.
' Set the Read, Write, Create, and All properties.
<RegistryPermissionAttribute(SecurityAction.PermitOnly, Read:="HKEY_LOCAL_MACHINE\HARDWARE"), _
RegistryPermissionAttribute(SecurityAction.PermitOnly, Write:="HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION"), _
RegistryPermissionAttribute(SecurityAction.PermitOnly, _
Create:="HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System"), _
RegistryPermissionAttribute(SecurityAction.PermitOnly, _
All:="HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor")> _
Public Shared Sub PermitOnlyMethod()
Console.WriteLine("Executing PermitOnlyMethod.")
Console.WriteLine(("Permitting the following accesses:" & ControlChars.Lf & ControlChars.Tab & "Read, KEY_LOCAL_MACHINE\HARDWARE" & ControlChars.Lf & ControlChars.Tab & "Write, HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION" & ControlChars.Lf & ControlChars.Tab & "Create, HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System" & ControlChars.Lf & ControlChars.Tab & "All, HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor"))
PermitOnlyTest()
End Sub 'PermitOnlyMethod
Public Shared Sub PermitOnlyTest()
Console.WriteLine("Executing PermitOnlyTest.")
Try
Dim ps As New PermissionSet(PermissionState.None)
ps.AddPermission(New RegistryPermission(RegistryPermissionAccess.Write, "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor"))
Console.WriteLine(("Demanding permission to write " & "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor"))
ps.Demand()
Console.WriteLine("Demand succeeded.")
ps.AddPermission(New RegistryPermission(RegistryPermissionAccess.Write, "HKEY_LOCAL_MACHINE\HARDWARE"))
Console.WriteLine(("Demanding permission to write to " & "HKEY_LOCAL_MACHINE\HARDWARE."))
' This demand should cause an exception.
ps.Demand()
' The TestFailed method is called if an exception is not thrown.
TestFailed()
Catch e As Exception
Console.WriteLine(("An exception was thrown because of a write demand: " & e.Message))
End Try
End Sub 'PermitOnlyTest
' This method demonstrates the the use of the RegistryPermission attribute to deny a permission.
<RegistryPermissionAttribute(SecurityAction.Deny, Write:="HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor")> _
Public Shared Sub DenyMethod()
Console.WriteLine("Executing DenyMethod.")
Console.WriteLine(("Denying permission to write to " & "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor."))
DenyTestMethod()
End Sub 'DenyMethod
Public Shared Sub DenyTestMethod()
Console.WriteLine("Executing DenyTestMethod.")
Try
Dim ps As New PermissionSet(PermissionState.None)
ps.AddPermission(New RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor"))
Console.WriteLine(("Demanding permission to read " & "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor."))
ps.Demand()
Console.WriteLine("Demand succeeded.")
ps.AddPermission(New RegistryPermission(RegistryPermissionAccess.Write, "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor"))
Console.WriteLine(("Demanding permission to write to " & "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor."))
' This demand should cause an exception.
ps.Demand()
' The TestFailed method is called if an exception is not thrown.
TestFailed()
Catch e As Exception
Console.WriteLine(("An exception was thrown because of a write demand: " & e.Message))
End Try
End Sub 'DenyTestMethod
' This method demonstrates the use of the EnvironmentPermissionAttribute to deny all permissions.
<RegistryPermissionAttribute(SecurityAction.Deny, Unrestricted:=True)> _
Public Shared Sub DenyAllMethod()
Console.WriteLine("Executing DenyAllMethod.")
Console.WriteLine("Denied all RegistryPermissions")
DenyAllTestMethod()
End Sub 'DenyAllMethod
' This method tests to assure permissions have been denied.
Public Shared Sub DenyAllTestMethod()
Console.WriteLine("Executing DenyAllTestMethod.")
Try
Dim ps As New PermissionSet(PermissionState.None)
ps.AddPermission(New RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor"))
Console.WriteLine(("Demanding permission to read " + "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor."))
' This demand should cause an exception.
ps.Demand()
' The TestFailed method is called if the expected exception is not thrown.
TestFailed()
Catch e As Exception
Console.WriteLine(("An exception was thrown because of a read demand: " + e.Message))
End Try
End Sub 'DenyAllTestMethod
Public Shared Sub TestFailed()
Console.WriteLine("In TestFailed method.")
Console.WriteLine("Throwing an exception.")
Throw New Exception()
End Sub 'TestFailed
Overloads Shared Sub Main(ByVal args() As String)
RegistryPermissionAttributeDemo()
End Sub 'Main
End Class '[MyClass]
[C#]
// This sample demonstrates the use of the RegistryPermissionAttribute.
using System;
using System.Reflection;
using System.Security.Permissions;
using System.Security;
using System.IO;
class MyClass
{
public static void RegistryPermissionAttributeDemo()
{
try
{
PermitOnlyMethod();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
try
{
DenyMethod();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
try
{
DenyAllMethod();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
// This method demonstrates the use of the RegistryPermissionAttribute to create PermitOnly permissions.
[RegistryPermissionAttribute(SecurityAction.PermitOnly,
Read = "HKEY_LOCAL_MACHINE\\HARDWARE")]
[RegistryPermissionAttribute(SecurityAction.PermitOnly,
Write = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION")]
[RegistryPermissionAttribute(SecurityAction.PermitOnly,
Create = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System")]
[RegistryPermissionAttribute(SecurityAction.PermitOnly,
All = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")]
public static void PermitOnlyMethod()
{
Console.WriteLine("Executing PermitOnlyMethod.");
Console.WriteLine("Permitting the following accesses:\n\t"
+ "Read, KEY_LOCAL_MACHINE\\HARDWARE\n\t"
+ "Write, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\n\t"
+ "Create, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\n\t"
+ "All, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor");
PermitOnlyTest();
}
public static void PermitOnlyTest()
{
Console.WriteLine("Executing PermitOnlyTest.");
try
{
PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission(
new RegistryPermission(RegistryPermissionAccess.Write,
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console.WriteLine("Demanding permission to write " +
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor");
ps.Demand();
Console.WriteLine("Demand succeeded.");
ps.AddPermission(
new RegistryPermission(RegistryPermissionAccess.Write,
"HKEY_LOCAL_MACHINE\\HARDWARE"));
Console.WriteLine("Demanding permission to write to " +
"HKEY_LOCAL_MACHINE\\HARDWARE.");
// This demand should cause an exception.
ps.Demand();
// The TestFailed method is called if an exception is not thrown.
TestFailed();
}
catch (Exception e)
{
Console.WriteLine("An exception was thrown because of a write demand: " + e.Message);
}
}
// This method demonstrates the the use of the RegistryPermission attribute to deny a permission.
[RegistryPermissionAttribute(SecurityAction.Deny,
Write = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")]
public static void DenyMethod()
{
Console.WriteLine("Executing DenyMethod.");
Console.WriteLine("Denying permission to write to " +
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
DenyTestMethod();
}
public static void DenyTestMethod()
{
Console.WriteLine("Executing DenyTestMethod.");
try
{
PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission(
new RegistryPermission(RegistryPermissionAccess.Read,
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console.WriteLine("Demanding permission to read " +
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
ps.Demand();
Console.WriteLine("Demand succeeded.");
ps.AddPermission(
new RegistryPermission(RegistryPermissionAccess.Write,
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console.WriteLine("Demanding permission to write to " +
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
// This demand should cause an exception.
ps.Demand();
// The TestFailed method is called if an exception is not thrown.
TestFailed();
}
catch (Exception e)
{
Console.WriteLine("An exception was thrown because of a write demand: " + e.Message);
}
}
// This method demonstrates the use of the EnvironmentPermissionAttribute to deny all permissions.
[RegistryPermissionAttribute(SecurityAction.Deny, Unrestricted = true)]
public static void DenyAllMethod()
{
Console.WriteLine("Executing DenyAllMethod.");
Console.WriteLine("Denied all RegistryPermissions");
DenyAllTestMethod();
}
// This method tests to assure permissions have been denied.
public static void DenyAllTestMethod()
{
Console.WriteLine("Executing DenyAllTestMethod.");
try
{
PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission(
new RegistryPermission(RegistryPermissionAccess.Read,
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console.WriteLine("Demanding permission to read " +
"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
// This demand should cause an exception.
ps.Demand();
// The TestFailed method is called if the expected exception is not thrown.
TestFailed();
}
catch (Exception e)
{
Console.WriteLine("An exception was thrown because of a read demand: " + e.Message);
}
}
public static void TestFailed()
{
Console.WriteLine("In TestFailed method.");
Console.WriteLine("Throwing an exception.");
throw new Exception();
}
static void Main(string[] args)
{
RegistryPermissionAttributeDemo();
}
}
[C++]
// This sample demonstrates the use of the RegistryPermissionAttribute.
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;
using namespace System::Security;
using namespace System::IO;
class MyClass {
public:
static void RegistryPermissionAttributeDemo() {
try {
PermitOnlyMethod();
} catch (Exception* e) {
Console::WriteLine(e->Message);
}
try {
DenyMethod();
} catch (Exception* e) {
Console::WriteLine(e->Message);
}
try {
DenyAllMethod();
} catch (Exception* e) {
Console::WriteLine(e->Message);
}
}
// This method demonstrates the use of the RegistryPermissionAttribute to create PermitOnly permissions.
public:
[RegistryPermissionAttribute(SecurityAction::PermitOnly,
Read = S"HKEY_LOCAL_MACHINE\\HARDWARE")]
[RegistryPermissionAttribute(SecurityAction::PermitOnly,
Write = S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION")]
[RegistryPermissionAttribute(SecurityAction::PermitOnly,
Create = S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System")]
[RegistryPermissionAttribute(SecurityAction::PermitOnly,
All = S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")]
static void PermitOnlyMethod() {
Console::WriteLine(S"Executing PermitOnlyMethod.");
Console::WriteLine(S"Permitting the following accesses:\n"
S"\t Read, KEY_LOCAL_MACHINE\\HARDWARE\n"
S"\t Write, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\n"
S"\t Create, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\n"
S"\t All, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor");
PermitOnlyTest();
}
public:
static void PermitOnlyTest() {
Console::WriteLine(S"Executing PermitOnlyTest.");
try {
PermissionSet* ps = new PermissionSet(PermissionState::None);
ps->AddPermission(new RegistryPermission(RegistryPermissionAccess::Write,
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console::WriteLine(S"Demanding permission to write "
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor");
ps->Demand();
Console::WriteLine(S"Demand succeeded.");
ps->AddPermission(new RegistryPermission(RegistryPermissionAccess::Write,
S"HKEY_LOCAL_MACHINE\\HARDWARE"));
Console::WriteLine(S"Demanding permission to write to HKEY_LOCAL_MACHINE\\HARDWARE.");
// This demand should cause an exception.
ps->Demand();
// The TestFailed method is called if an exception is not thrown.
TestFailed();
} catch (Exception* e) {
Console::WriteLine(S"An exception was thrown because of a write demand: {0}", e->Message);
}
}
public:
// This method demonstrates the the use of the RegistryPermission attribute to deny a permission.
[RegistryPermissionAttribute(SecurityAction::Deny,
Write = S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")]
static void DenyMethod() {
Console::WriteLine(S"Executing DenyMethod.");
Console::WriteLine(S"Denying permission to write to "
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
DenyTestMethod();
}
public:
static void DenyTestMethod() {
Console::WriteLine(S"Executing DenyTestMethod.");
try {
PermissionSet* ps = new PermissionSet(PermissionState::None);
ps->AddPermission(new RegistryPermission(RegistryPermissionAccess::Read,
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console::WriteLine(S"Demanding permission to read "
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
ps->Demand();
Console::WriteLine(S"Demand succeeded.");
ps->AddPermission(new RegistryPermission(RegistryPermissionAccess::Write,
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console::WriteLine(S"Demanding permission to write to "
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
// This demand should cause an exception.
ps->Demand();
// The TestFailed method is called if an exception is not thrown.
TestFailed();
} catch (Exception* e) {
Console::WriteLine(S"An exception was thrown because of a write demand: {0}", e->Message);
}
}
public:
// This method demonstrates the use of the EnvironmentPermissionAttribute to deny all permissions.
[RegistryPermissionAttribute(SecurityAction::Deny, Unrestricted = true)]
static void DenyAllMethod() {
Console::WriteLine(S"Executing DenyAllMethod.");
Console::WriteLine(S"Denied all RegistryPermissions");
DenyAllTestMethod();
}
// This method tests to assure permissions have been denied.
public:
static void DenyAllTestMethod() {
Console::WriteLine(S"Executing DenyAllTestMethod.");
try {
PermissionSet* ps = new PermissionSet(PermissionState::None);
ps->AddPermission(new RegistryPermission(RegistryPermissionAccess::Read,
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor"));
Console::WriteLine(S"Demanding permission to read "
S"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor.");
// This demand should cause an exception.
ps->Demand();
// The TestFailed method is called if the expected exception is not thrown.
TestFailed();
} catch (Exception* e) {
Console::WriteLine(S"An exception was thrown because of a read demand: {0}", e->Message);
}
}
public:
static void TestFailed() {
Console::WriteLine(S"In TestFailed method.");
Console::WriteLine(S"Throwing an exception.");
throw new Exception();
}
};
int main() {
MyClass::RegistryPermissionAttributeDemo();
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
RegistryPermissionAttribute クラス | RegistryPermissionAttribute メンバ | System.Security.Permissions 名前空間