RegistryPermissionAttributeとは? わかりやすく解説

RegistryPermissionAttribute クラス

宣言セキュリティ使用して、RegistryPermission のセキュリティ アクションコード適用できるようにします。このクラス継承できません。

名前空間: System.Security.Permissions
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class
 Or AttributeTargets.Struct Or AttributeTargets.Constructor
 Or AttributeTargets.Method, AllowMultiple:=True, Inherited:=False)> _
Public NotInheritable Class
 RegistryPermissionAttribute
    Inherits CodeAccessSecurityAttribute
Dim instance As RegistryPermissionAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method,
 AllowMultiple=true, Inherited=false)] 
public sealed class RegistryPermissionAttribute
 : CodeAccessSecurityAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Constructor|AttributeTargets::Method,
 AllowMultiple=true, Inherited=false)] 
public ref class RegistryPermissionAttribute
 sealed : public CodeAccessSecurityAttribute
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method,
 AllowMultiple=true, Inherited=false) */ 
public final class RegistryPermissionAttribute
 extends CodeAccessSecurityAttribute
SerializableAttribute 
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method,
 AllowMultiple=true, Inherited=false) 
public final class RegistryPermissionAttribute
 extends CodeAccessSecurityAttribute
解説解説
使用例使用例

指定したレジストリ値の状態へのアクセスのために RegistryPermission要求する正し方法示しコード実行するためには少なくともこのアクセス許可が必要であることを次の宣言属性の例に示します

 <Assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, _
 Read := "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor")>
'In Visual Basic, you must specify that you are using the assembly scope
 when making a request.
[assembly:RegistryPermissionAttribute(SecurityAction.RequestMinimum,
Read="HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")]
//In C#, you must specify that you are using the assembly scope when
 making a request.
[assembly:RegistryPermissionAttribute(SecurityAction::RequestMinimum,
Read="HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")];
//In C++, you must specify that you are using the assembly scope when
 making a request.
/** @assembly RegistryPermissionAttribute(SecurityAction.RequestMinimum, 
    Read = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor")
 */
// In VJ#, you must specify that you are using the assembly scope when
 
// making a request.

リンク時に、呼び出し元のコード無制限RegistryPermission含めることを要求する方法次の例に示します通常メソッドクラス不正なコードから保護するための要求マネージ ライブラリ (DLL) で作成されます。

<RegistryPermissionAttribute(SecurityAction.Demand, _
 Unrestricted := True)> Public Class
 SampleClass
[RegistryPermissionAttribute(SecurityAction.Demand, Unrestricted=true)]
[RegistryPermissionAttribute(SecurityAction::Demand,Unrestricted=true)]
/** @attribute RegistryPermissionAttribute(SecurityAction.Demand,
    Unrestricted = true)
 */
継承階層継承階層
System.Object
   System.Attribute
     System.Security.Permissions.SecurityAttribute
       System.Security.Permissions.CodeAccessSecurityAttribute
        System.Security.Permissions.RegistryPermissionAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryPermissionAttribute メンバ
System.Security.Permissions 名前空間
RegistryPermission クラス
RegistryPermissionAccess 列挙
その他の技術情報
属性使用したメタデータ拡張

RegistryPermissionAttribute コンストラクタ

SecurityAction指定して、RegistryPermissionAttribute クラス新しインスタンス初期化します。

名前空間: System.Security.Permissions
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    action As SecurityAction _
)
Dim action As SecurityAction

Dim instance As New RegistryPermissionAttribute(action)
public RegistryPermissionAttribute (
    SecurityAction action
)
public:
RegistryPermissionAttribute (
    SecurityAction action
)
public RegistryPermissionAttribute (
    SecurityAction action
)
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] 
// 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();
    }

}
// This sample demonstrates the use of the RegistryPermissionAttribute.
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.
   [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")]

   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();
   }

   static void PermitOnlyTest()
   {
      Console::WriteLine( "Executing PermitOnlyTest." );
      try
      {
         PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
         ps->AddPermission( gcnew 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( gcnew 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:
 {0}", 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")]
   static void DenyMethod()
   {
      Console::WriteLine( "Executing DenyMethod." );
      Console::WriteLine( "Denying permission to write to "
      "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor."
 );
      DenyTestMethod();
   }

   static void DenyTestMethod()
   {
      Console::WriteLine( "Executing DenyTestMethod." );
      try
      {
         PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
         ps->AddPermission( gcnew 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( gcnew 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:
 {0}", e->Message );
      }

   }


   // This method demonstrates the use of the EnvironmentPermissionAttribute
 to deny all permissions.

   [RegistryPermissionAttribute(SecurityAction::Deny,Unrestricted=true)]
   static void DenyAllMethod()
   {
      Console::WriteLine( "Executing DenyAllMethod." );
      Console::WriteLine( "Denied all RegistryPermissions" );
      DenyAllTestMethod();
   }


   // This method tests to assure permissions have been denied.
   static void DenyAllTestMethod()
   {
      Console::WriteLine( "Executing DenyAllTestMethod." );
      try
      {
         PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
         ps->AddPermission( gcnew 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:
 {0}", e->Message );
      }

   }

   static void TestFailed()
   {
      Console::WriteLine( "In TestFailed method." );
      Console::WriteLine( "Throwing an exception." );
      throw gcnew Exception;
   }

};

int main()
{
   MyClass::RegistryPermissionAttributeDemo();
}

// This sample demonstrates the use of the RegistryPermissionAttribute.
import System.*;
import System.Reflection.*;
import System.Security.Permissions.*;
import System.Security.*;
import System.IO.*;

class MyClass
{
    public static void RegistryPermissionAttributeDemo()
 
    {
        try {
            PermitOnlyMethod();
        }
        catch(System.Exception e) {
            Console.WriteLine(e.get_Message().toString());
        }
        try {
            DenyMethod();
        }
        catch(System.Exception e) {
            Console.WriteLine(e.get_Message().toString());
        }
        try {
            DenyAllMethod();
        }
        catch(System.Exception e) {
            Console.WriteLine(e.get_Message().toString());
        }
    } //RegistryPermissionAttributeDemo   

    // This method demonstrates the use of the RegistryPermissionAttribute
    // to create PermitOnly permissions.
    /** @attribute RegistryPermissionAttribute(SecurityAction.PermitOnly,
        Read = "HKEY_LOCAL_MACHINE\\HARDWARE")
     */

    /** @attribute RegistryPermissionAttribute(SecurityAction.PermitOnly,
        Write = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION")
     */
    
    /** @attribute RegistryPermissionAttribute(SecurityAction.PermitOnly,
        Create = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System")
     */
    
    /** @attribute 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();
    } //PermitOnlyMethod

    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(System.Exception e) {
            Console.WriteLine(("An exception was thrown because of a write"
 
                + " demand: " + e.get_Message()));
        }
    } //PermitOnlyTest   

    // This method demonstrates the the use of the RegistryPermission
    // attribute to deny a permission.

    /** @attribute 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();
    } //DenyMethod


    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(System.Exception e) {
            Console.WriteLine(("An exception was thrown because " 
                + "of a write demand: " + e.get_Message()));
        }
    } //DenyTestMethod   

    // This method demonstrates the use of the EnvironmentPermissionAttribute
    // to deny all permissions.

    /** @attribute RegistryPermissionAttribute(SecurityAction.Deny,
        Unrestricted = true)
     */
    
    public static void DenyAllMethod()
 
    {
        Console.WriteLine("Executing DenyAllMethod.");
        Console.WriteLine("Denied all RegistryPermissions");
        DenyAllTestMethod();
    } //DenyAllMethod

    // 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(System.Exception e) {
            Console.WriteLine(("An exception was thrown because " 
                + " of a read demand: " + e.get_Message()));
        }
    } //DenyAllTestMethod

    public static void TestFailed()
 throws Exception
    {
        Console.WriteLine("In TestFailed method.");
        Console.WriteLine("Throwing an exception.");
        throw new Exception() ;
    } //TestFailed
        
    public static void main(String[]
 args)
    {
        RegistryPermissionAttributeDemo();
    } //main
} //MyClass 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryPermissionAttribute クラス
RegistryPermissionAttribute メンバ
System.Security.Permissions 名前空間

RegistryPermissionAttribute プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

RegistryPermissionAttribute クラス
System.Security.Permissions 名前空間
RegistryPermission クラス
RegistryPermissionAccess 列挙

その他の技術情報

属性使用したメタデータ拡張

RegistryPermissionAttribute メソッド


パブリック メソッドパブリック メソッド

  名前 説明
パブリック メソッド CreatePermission オーバーライドされます新しい RegistryPermission を作成して返します
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用され指定した型のカスタム属性取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されカスタム属性配列取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスハッシュ コード返します。 ( Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラス既定値かどうか示します。 ( Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます指定した型のカスタム属性が、アセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されているかどうか判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンス等しかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String返します。 ( Object から継承されます。)
参照参照

関連項目

RegistryPermissionAttribute クラス
System.Security.Permissions 名前空間
RegistryPermission クラス
RegistryPermissionAccess 列挙

その他の技術情報

属性使用したメタデータ拡張

RegistryPermissionAttribute メンバ

宣言セキュリティ使用して、RegistryPermission のセキュリティ アクションコード適用できるようにします。このクラス継承できません。

RegistryPermissionAttribute データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド RegistryPermissionAttribute SecurityAction を指定して、RegistryPermissionAttribute クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド CreatePermission オーバーライドされます新しRegistryPermission作成して返します
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用され指定した型のカスタム属性取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されカスタム属性配列取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスハッシュ コード返します。 (Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラス既定値かどうか示します。 (Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます指定した型のカスタム属性が、アセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されているかどうか判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンス等しかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String返します。 (Object から継承されます。)
参照参照

関連項目

RegistryPermissionAttribute クラス
System.Security.Permissions 名前空間
RegistryPermission クラス
RegistryPermissionAccess 列挙

その他の技術情報

属性使用したメタデータ拡張



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「RegistryPermissionAttribute」の関連用語

RegistryPermissionAttributeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



RegistryPermissionAttributeのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS