EnvironmentPermissionAttribute コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > EnvironmentPermissionAttribute コンストラクタの意味・解説 

EnvironmentPermissionAttribute コンストラクタ

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

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

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

Dim instance As New EnvironmentPermissionAttribute(action)
public EnvironmentPermissionAttribute (
    SecurityAction action
)
public:
EnvironmentPermissionAttribute (
    SecurityAction action
)
public EnvironmentPermissionAttribute (
    SecurityAction action
)
public function EnvironmentPermissionAttribute
 (
    action : SecurityAction
)

パラメータ

action

SecurityAction 値の 1 つ

例外例外
例外種類条件

ArgumentException

action パラメータが、SecurityAction の有効値ではありません。

使用例使用例
' This sample demonstrates the use of the EnvironmentPermissionAttribute.
Imports System
Imports System.Reflection
Imports System.Security.Permissions
Imports System.Security
Imports System.IO
Imports Microsoft.VisualBasic



Class [MyClass]

    Public Shared Sub PermissionDemo()
        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 'PermissionDemo


    ' This method demonstrates the use of the EnvironmentPermissionAttribute
 to create a PermitOnly permission.
    ' Set the Read property for a PermitOnly SecurityAction.
    ' Set the All property for a PermitOnly SecurityAction.
    ' Set the Read, All, and Write properties.
    <EnvironmentPermissionAttribute(SecurityAction.PermitOnly, Read:="COMPUTERNAME"),
 _
    EnvironmentPermissionAttribute(SecurityAction.PermitOnly, All:="USERNAME"),
 _
    EnvironmentPermissionAttribute(SecurityAction.PermitOnly, Write:="USERDOMAIN")>
 _
    Public Shared Sub PermitOnlyMethod()
        Console.WriteLine("Executing PermitOnlyMethod.")
        Console.WriteLine("PermitOnly the Read permission for
 COMPUTERNAME.")
        Console.WriteLine("PermitOnly the All permission for USERNAME.")
        Console.WriteLine("PermitOnly the Write permission for
 USERDOMAIN.")

        PermitOnlyTest()
    End Sub 'PermitOnlyMethod


    Public Shared Sub PermitOnlyTest()
        Console.WriteLine("Executing PermitOnlyTest.")
        Try
            Dim ps As New
 PermissionSet(PermissionState.None)
            ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "USERNAME"))
            Console.WriteLine("Demanding permission to read USERNAME.")
            ps.Demand()
            Console.WriteLine("Demand succeeded.")
            ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Write,
 "COMPUTERNAME"))
            Console.WriteLine("Demanding permission to write COMPUTERNAME.")
            ' 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 use of the EnvironmentPermission
 attribute to deny a permission. 
    <EnvironmentPermissionAttribute(SecurityAction.Deny, Read:="USERNAME")>
 _
    Public Shared Sub DenyMethod()
        Console.WriteLine("Executing DenyMethod.")
        Console.WriteLine("Denying the Read permission for USERNAME.")

        DenyTestMethod()
    End Sub 'DenyMethod


    Public Shared Sub DenyTestMethod()
        Console.WriteLine("Executing DenyTestMethod.")
        Try
            Dim ps As New
 PermissionSet(PermissionState.None)
            ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "COMPUTERNAME"))
            Console.WriteLine("Demanding permission to read COMPUTERNAME.")
            ps.Demand()
            Console.WriteLine("Demand succeeded.")
            ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "USERNAME"))
            Console.WriteLine("Demanding permission to read USERNAME.")
            ' 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 read demand: " & e.Message))
        End Try
    End Sub 'DenyTestMethod


    ' This method demonstrates the use of the EnvironmentPermissionAttribute
 to deny all permissions.
    <EnvironmentPermissionAttribute(SecurityAction.Deny, Unrestricted:=True)>
 _
    Public Shared Sub DenyAllMethod()
        Console.WriteLine("Executing DenyAllMethod.")
        Console.WriteLine("Denied all EnvironmentPermissions")

        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 EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "COMPUTERNAME"))
            Console.WriteLine("Demanding permission to read
 'COMPUTERNAME'.")
            ' 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("Executing TestFailed method.")
        Console.WriteLine("Throwing an exception.")
        Throw New Exception()
    End Sub 'TestFailed

    Overloads Shared Sub
 Main(ByVal args() As String)
        PermissionDemo()
    End Sub 'Main
End Class '[MyClass] 
// This sample demonstrates the use of the EnvironmentPermissionAttribute.

using System;
using System.Reflection;
using System.Security.Permissions;
using System.Security;
using System.IO;

    class MyClass
    {
        public static void
 PermissionDemo()
        {  
            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 EnvironmentPermissionAttribute
 to create a PermitOnly permission.

        // Set the Read property for a PermitOnly SecurityAction.
        [EnvironmentPermissionAttribute(SecurityAction.PermitOnly, Read = "COMPUTERNAME")]
 
        // Set the All property for a PermitOnly SecurityAction.
        [EnvironmentPermissionAttribute(SecurityAction.PermitOnly, All = "USERNAME")]
        // Set the Write property for a PermitOnly SecurityAction.
        [EnvironmentPermissionAttribute(SecurityAction.PermitOnly, Write = "USERDOMAIN")]
        public static void
 PermitOnlyMethod()
        {
            Console.WriteLine("Executing PermitOnlyMethod.");
            Console.WriteLine("PermitOnly the Read permission for
 COMPUTERNAME.");
            Console.WriteLine("PermitOnly the All permission for
 USERNAME.");
            Console.WriteLine("PermitOnly the Write permission for
 USERDOMAIN.");

            PermitOnlyTest();
        }
    
        public static void
 PermitOnlyTest()
        {
            Console.WriteLine("Executing PermitOnlyTest.");
            try
            {
                PermissionSet ps = new PermissionSet(PermissionState.None);
                ps.AddPermission(
                    new EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "USERNAME"));
                Console.WriteLine("Demanding permission to read USERNAME.");
                ps.Demand();
                Console.WriteLine("Demand succeeded.");
                ps.AddPermission(
                    new EnvironmentPermission(EnvironmentPermissionAccess.Write,
 
                    "COMPUTERNAME"));
                Console.WriteLine("Demanding permission to write COMPUTERNAME.");
                // 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 EnvironmentPermission attribute
 to deny a permission. 
        [EnvironmentPermissionAttribute(SecurityAction.Deny, Read = "USERNAME")]
        public static void
 DenyMethod()
        {
            Console.WriteLine("Executing DenyMethod.");
            Console.WriteLine("Denying the Read permission for
 USERNAME.");

            DenyTestMethod();
        }

        public static void
 DenyTestMethod()
        {
            Console.WriteLine("Executing DenyTestMethod.");
            try
            {
                PermissionSet ps = new PermissionSet(PermissionState.None);
                ps.AddPermission(
                    new EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "COMPUTERNAME"));
                Console.WriteLine("Demanding permission to read COMPUTERNAME.");
                ps.Demand();
                Console.WriteLine("Demand succeeded.");
                ps.AddPermission(
                    new EnvironmentPermission(EnvironmentPermissionAccess.Read,
 
                    "USERNAME"));
                Console.WriteLine("Demanding permission to read USERNAME.");
                // 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 read
 demand: " + e.Message);
            }
        }

        // This method demonstrates the use of the EnvironmentPermissionAttribute
 to deny all permissions.
        [EnvironmentPermissionAttribute(SecurityAction.Deny, Unrestricted = true)]
        public static void
 DenyAllMethod()
        {
            Console.WriteLine("Executing DenyAllMethod.");
            Console.WriteLine("Denied all EnvironmentPermissions");

            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 EnvironmentPermission(EnvironmentPermissionAccess.Read,
 "COMPUTERNAME"));
                Console.WriteLine("Demanding permission to read 'COMPUTERNAME'.");
                // 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("Executing TestFailed method.");
            Console.WriteLine("Throwing an exception.");
            throw new Exception();
        }

        static void Main(string[]
 args)
        {
            PermissionDemo();
        }

    }
// This sample demonstrates the use of the EnvironmentPermissionAttribute.
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;
using namespace System::Security;
using namespace System::IO;
void TestFailed()
{
   Console::WriteLine( "Executing TestFailed method." );
   Console::WriteLine( "Throwing an exception." );
   throw gcnew Exception;
}

void PermitOnlyTest()
{
   Console::WriteLine( "Executing PermitOnlyTest." );
   try
   {
      PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
      ps->AddPermission( gcnew EnvironmentPermission( EnvironmentPermissionAccess::Read,"USERNAME"
 ) );
      Console::WriteLine( "Demanding permission to read USERNAME." );
      ps->Demand();
      Console::WriteLine( "Demand succeeded." );
      ps->AddPermission( gcnew EnvironmentPermission( EnvironmentPermissionAccess::Write,"COMPUTERNAME"
 ) );
      Console::WriteLine( "Demanding permission to write COMPUTERNAME."
 );
      
      // 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 function demonstrates the use of the EnvironmentPermissionAttribute
 to create a PermitOnly permission.
// Set the Read property for a PermitOnly SecurityAction.
[EnvironmentPermissionAttribute(SecurityAction::PermitOnly,Read="COMPUTERNAME")]
// Set the All property for a PermitOnly SecurityAction.
[EnvironmentPermissionAttribute(SecurityAction::PermitOnly,All="USERNAME")]
// Set the Write property for a PermitOnly SecurityAction.
[EnvironmentPermissionAttribute(SecurityAction::PermitOnly,Write="USERDOMAIN")]

void PermitOnlyMethod()
{
   Console::WriteLine( "Executing PermitOnlyMethod." );
   Console::WriteLine( "PermitOnly the Read permission for
 COMPUTERNAME." );
   Console::WriteLine( "PermitOnly the All permission for
 USERNAME." );
   Console::WriteLine( "PermitOnly the Write permission for
 USERDOMAIN." );
   PermitOnlyTest();
}

void DenyTestMethod()
{
   Console::WriteLine( "Executing DenyTestMethod." );
   try
   {
      PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
      ps->AddPermission( gcnew EnvironmentPermission( EnvironmentPermissionAccess::Read,"COMPUTERNAME"
 ) );
      Console::WriteLine( "Demanding permission to read COMPUTERNAME."
 );
      ps->Demand();
      Console::WriteLine( "Demand succeeded." );
      ps->AddPermission( gcnew EnvironmentPermission( EnvironmentPermissionAccess::Read,"USERNAME"
 ) );
      Console::WriteLine( "Demanding permission to read USERNAME." );
      
      // 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 read demand:
 {0}", e->Message );
   }

}


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

[EnvironmentPermissionAttribute(SecurityAction::Deny,Read="USERNAME")]
void DenyMethod()
{
   Console::WriteLine( "Executing DenyMethod." );
   Console::WriteLine( "Denying the Read permission for USERNAME."
 );
   DenyTestMethod();
}

void PermissionDemo()
{
   try
   {
      PermitOnlyMethod();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

   try
   {
      DenyMethod();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

int main()
{
   PermissionDemo();
}

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

class MyClass
{
    public static void PermissionDemo()
    {
        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());
        }
    } //PermissionDemo

    // This method demonstrates the use of the EnvironmentPermissionAttribute
 to 
    // create a PermitOnly permission.
    // Set the Read property for a PermitOnly SecurityAction.
    /** @attribute EnvironmentPermissionAttribute(SecurityAction.PermitOnly, 
        Read = "COMPUTERNAME")
     */

    // Set the All property for a PermitOnly SecurityAction.
    /** @attribute EnvironmentPermissionAttribute(SecurityAction.PermitOnly, 
        All = "USERNAME")
     */

    // Set the Write property for a PermitOnly SecurityAction.
    /** @attribute EnvironmentPermissionAttribute(SecurityAction.PermitOnly, 
        Write = "USERDOMAIN")
     */
    public static void PermitOnlyMethod()
    {
        Console.WriteLine("Executing PermitOnlyMethod.");
        Console.WriteLine("PermitOnly the Read permission for
 COMPUTERNAME.");
        Console.WriteLine("PermitOnly the All permission for
 USERNAME.");
        Console.WriteLine("PermitOnly the Write permission for
 USERDOMAIN.");
        PermitOnlyTest();
    } //PermitOnlyMethod

    public static void PermitOnlyTest()
    {
        Console.WriteLine("Executing PermitOnlyTest.");
        try {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new EnvironmentPermission(
                EnvironmentPermissionAccess.Read, "USERNAME"));
            Console.WriteLine("Demanding permission to read USERNAME.");
            ps.Demand();
            Console.WriteLine("Demand succeeded.");
            ps.AddPermission(new EnvironmentPermission(
                EnvironmentPermissionAccess.Write, "COMPUTERNAME"));
            Console.WriteLine("Demanding permission to write COMPUTERNAME.");

            // 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 use of the EnvironmentPermission
 
    // attribute to deny a permission. 
    /** @attribute EnvironmentPermissionAttribute
        (SecurityAction.Deny, Read = "USERNAME")
     */
    public static void DenyMethod()
    {
        Console.WriteLine("Executing DenyMethod.");
        Console.WriteLine("Denying the Read permission for
 USERNAME.");
        DenyTestMethod();
    } //DenyMethod

    public static void DenyTestMethod()
    {
        Console.WriteLine("Executing DenyTestMethod.");
        try {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new EnvironmentPermission(
                EnvironmentPermissionAccess.Read, "COMPUTERNAME"));
            Console.WriteLine("Demanding permission to read COMPUTERNAME.");
            ps.Demand();
            Console.WriteLine("Demand succeeded.");
            ps.AddPermission(new EnvironmentPermission(
                EnvironmentPermissionAccess.Read, "USERNAME"));
            Console.WriteLine("Demanding permission to read USERNAME.");

            // 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 read demand: " + e.get_Message()));
        }
    } //DenyTestMethod

    // This method demonstrates the use of the 
    //EnvironmentPermissionAttribute to deny all permissions.
    /** @attribute EnvironmentPermissionAttribute
        (SecurityAction.Deny, Unrestricted = true)
     */
    public static void DenyAllMethod()
    {
        Console.WriteLine("Executing DenyAllMethod.");
        Console.WriteLine("Denied all EnvironmentPermissions");
        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 EnvironmentPermission(
                EnvironmentPermissionAccess.Read, "COMPUTERNAME"));
            Console.WriteLine("Demanding permission to read 'COMPUTERNAME'.");

            // 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("Executing TestFailed method.");
        Console.WriteLine("Throwing an exception.");
        throw new Exception();
    } //TestFailed

    public static void main(String[]
 args)
    {
        PermissionDemo();
    } //main
} //MyClass 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
EnvironmentPermissionAttribute クラス
EnvironmentPermissionAttribute メンバ
System.Security.Permissions 名前空間



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

辞書ショートカット

すべての辞書の索引

「EnvironmentPermissionAttribute コンストラクタ」の関連用語

EnvironmentPermissionAttribute コンストラクタのお隣キーワード
検索ランキング

   

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



EnvironmentPermissionAttribute コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS