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

DataProtectionPermission クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

暗号化されたデータメモリアクセスする権利制御します。このクラス継承できません。

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

<SerializableAttribute> _
Public NotInheritable Class
 DataProtectionPermission
    Inherits CodeAccessPermission
    Implements IUnrestrictedPermission
Dim instance As DataProtectionPermission
[SerializableAttribute] 
public sealed class DataProtectionPermission
 : CodeAccessPermission, IUnrestrictedPermission
[SerializableAttribute] 
public ref class DataProtectionPermission sealed
 : public CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
public final class DataProtectionPermission
 extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
public final class DataProtectionPermission
 extends CodeAccessPermission implements IUnrestrictedPermission
解説解説

このアクセス許可は、ProtectedData クラスおよび ProtectedMemory クラス使用してデータメモリ暗号化する権利制御するために使用します

使用例使用例

DataProtectionPermission クラスメンバ使用する方法次のコード例示します

Imports System
Imports System.Security.Permissions
Imports System.Security.Cryptography
Imports System.Security
Imports System.IO

<Assembly: DataProtectionPermission( _
    SecurityAction.RequestMinimum, _
    Flags:=DataProtectionPermissionFlags.ProtectData)> 

Public Class Form1
    Inherits System.Windows.Forms.Form

    ' Create a byte array for additional entropy when using the Protect
    ' and Unprotect methods.
    Private s_aditionalEntropy() As Byte
 = {9, 8, 7, 6, 5}

    Private encryptedSecret() As Byte
    Private originalData() As Byte

    ' Event handler for Run button.
    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles
 Button1.Click

        tbxOutput.Cursor = Cursors.WaitCursor
        tbxOutput.Text = ""

        Write("Creating a permission with the Flags property =
 ")
        WriteLine("ProtectData.")
        Dim sp As New DataProtectionPermission(
 _
            DataProtectionPermissionFlags.ProtectData)
        ' Deny the permission to protect data.
        sp.Deny()
        ' The following code results in an exception due to an attempt
        ' to protect data.
        ProtectData()
        ' Remove the Deny for ProtectData permission.
        CodeAccessPermission.RevertDeny()
        ' The call to protect data will now succeed.
        ProtectData()
        Dim sp2 As New DataProtectionPermission(
 _
            DataProtectionPermissionFlags.UnprotectData)
        sp2.Deny()
        ' The following code results in an exception due to an attempt
        ' to unprotect data.
        UnprotectData()
        ' Remove the Deny for UnprotectData permission.
        CodeAccessPermission.RevertDeny()

        UnprotectData()
        ' Demonstrate the attribute.
        TryProtectData()
        ' Demonstrate the behavior of the class members.
        ShowMembers()

        ' Reset the cursor and conclude application.
        WriteLine(vbCrLf + "This sample completed successfully;"
 + _
            " press Exit to continue.")
        tbxOutput.Cursor = Cursors.Default
    End Sub

    'Deny the permission the ability to add to a store.
    <DataProtectionPermission( _
        SecurityAction.Deny, _
        Flags:=DataProtectionPermissionFlags.ProtectData)> _
    Private Sub TryProtectData()
        Try
            ProtectData()
        Catch ex As SecurityException
            Dim protectionPermission As DataProtectionPermission
            protectionPermission = _
                CType(ex.DenySetInstance, DataProtectionPermission)

            WriteLine("Security exception thrown when attempting:
 " + _
                protectionPermission.Flags.ToString())
        End Try
    End Sub
    ' The following method is intended to demonstrate only the behavior
 of
    ' DataProtectionPermission class members,and not their practical
 usage.
    ' Most properties and methods in this class are used for the resolution
    ' and enforcement of security policy by the security infrastructure
 code.
    Private Sub ShowMembers()
        WriteLine("Creating four DataProtectionPermissions.")
        Write("Creating the first permission with the Flags property
 = ")
        WriteLine("ProtectData.")
        Dim sp1 As New DataProtectionPermission(
 _
            DataProtectionPermissionFlags.ProtectData)

        Write("Creating the second permission with the Flags property
 = ")
        WriteLine("AllFlags.")
        Dim sp2 As New DataProtectionPermission(
 _
            DataProtectionPermissionFlags.AllFlags)

        Write("Creating the third permission with a permission
 state = ")
        WriteLine("Unrestricted.")
        Dim sp3 As New DataProtectionPermission(PermissionState.Unrestricted)
        Write("Creating the fourth permission with a permission
 state = ")
        WriteLine("None.")
        Dim sp4 As New DataProtectionPermission(PermissionState.None)

        Write("Is the permission with all flags set (AllFlags)
 a subset of ")
        Write("the permission with an Unrestricted permission
 state?")
        If (sp2.IsSubsetOf(sp3)) Then
            WriteLine("Yes")
        Else
            WriteLine("No")
        End If

        Write("Is the permission with ProtectData access a subset
 of the")
        Write(" permission with AllFlags set? ")
        If (sp1.IsSubsetOf(sp2)) Then
            WriteLine("Yes")
        Else
            WriteLine("No")
        End If

        Write("Is the third permission unrestricted? ")
        If (sp3.IsUnrestricted()) Then
            WriteLine("Yes")
        Else
            WriteLine("No")
        End If

        WriteLine("Copying the second permission to the fourth
 permission.")
        sp4 = CType(sp2.Copy(), DataProtectionPermission)

        Write("Is the fourth permission equal to the second permission?
 ")
        If (sp4.Equals(sp2)) Then
            WriteLine("Yes")
        Else
            WriteLine("No")
        End If

        Write("Creating the intersection of the second and first")
        WriteLine("permissions.")
        sp4 = CType(sp2.Intersect(sp1), DataProtectionPermission)
        WriteLine("The value of the Flags property is: "
 + _
            sp4.Flags.ToString())

        WriteLine("Creating the union of the second and first
 permissions.")
        sp4 = CType(sp2.Union(sp1), DataProtectionPermission)
        Write("Result of the union of the second permission with
 the first: ")
        WriteLine(sp4.Flags.ToString())

        WriteLine("Using an XML roundtrip to reset the fourth
 permission.")
        sp4.FromXml(sp2.ToXml())
        Write("Does the XML roundtrip result equal the original
 permission? ")
        If (sp4.Equals(sp2)) Then
            WriteLine("Yes")
        Else
            WriteLine("No")
        End If
    End Sub

    ' Create a simple byte array containing data to be encrypted.
    Public Sub ProtectData()
        Dim secret() As Byte
 = {0, 1, 2, 3, 4, 1, 2, 3, 4}

        ' Encrypt the data.
        encryptedSecret = Protect(secret)
        If (Not encryptedSecret Is
 Nothing) Then
            WriteLine("The encrypted byte array is:")
            PrintValues(encryptedSecret)
        End If
    End Sub

    ' Decrypt the data and store in a byte array.
    Public Sub UnprotectData()
        originalData = Unprotect(encryptedSecret)

        If (Not originalData Is
 Nothing) Then
            WriteLine("The original data is:" + Environment.NewLine)
            PrintValues(originalData)
        End If
    End Sub

    ' Encrypt data in the specified byte array.
    Public Function Protect(ByVal
 data() As Byte) As Byte()
        Try
            ' Encrypt the data using DataProtectionScope.CurrentUser.
 The
            ' result can be decrypted only by the user who encrypted
 the data.
            Return ProtectedData.Protect( _
                data, _
                s_aditionalEntropy, _
                DataProtectionScope.CurrentUser)

        Catch ex As CryptographicException
            WriteLine("Data was not encrypted. An error has occurred.")
            WriteLine(ex.ToString())
            Return Nothing

        Catch securityException As SecurityException
            WriteLine("Insufficient permissions. An error has
 occurred.")
            WriteLine(securityException.ToString())
            Return Nothing

        End Try
    End Function
    Public Function Unprotect(ByVal
 data() As Byte) As Byte()
        Try
            ' Decrypt the data using DataProtectionScope.CurrentUser.
            ' The result can be decrypted only by the same current user.
            Return ProtectedData.Unprotect( _
                data, _
                s_aditionalEntropy, _
                DataProtectionScope.CurrentUser)

        Catch ex As CryptographicException
            WriteLine("Data was not decrypted. An error has occurred.")
            WriteLine(ex.ToString())
            Return Nothing

        Catch securityException As SecurityException
            WriteLine("Insufficient permissions. An error has
 occurred.")
            WriteLine(securityException.ToString())
            Return Nothing

        End Try
    End Function
    Public Sub PrintValues(ByVal
 myArr() As Byte)
        For Each i As Byte
 In myArr
            Write("    " + i.ToString())
        Next
        WriteLine("")
    End Sub
    ' Write the specified message and a carriage return to the output
 textbox.
    Private Sub WriteLine(ByVal
 message As String)
        tbxOutput.AppendText(message + vbCrLf)
    End Sub
    ' Write the specified message to the output textbox.
    Private Sub Write(ByVal
 message As String)
        tbxOutput.AppendText(message)
    End Sub

    ' Event handler for Exit button.
    Private Sub Button2_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles
 Button2.Click

        Application.Exit()
    End Sub
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides
 Sub Dispose(ByVal disposing As
 Boolean)
        If disposing Then
            If Not (components Is
 Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel2 As
 System.Windows.Forms.Panel
    Friend WithEvents Panel1 As
 System.Windows.Forms.Panel
    Friend WithEvents Button1 As
 System.Windows.Forms.Button
    Friend WithEvents Button2 As
 System.Windows.Forms.Button
    Friend WithEvents tbxOutput As
 System.Windows.Forms.RichTextBox
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.tbxOutput = New System.Windows.Forms.RichTextBox
        Me.Panel2.SuspendLayout()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel2
        '
        Me.Panel2.Controls.Add(Me.Button1)
        Me.Panel2.Controls.Add(Me.Button2)
        Me.Panel2.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Panel2.Location = New System.Drawing.Point(0,
 277)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Padding = New System.Windows.Forms.Padding(20)
        Me.Panel2.Size = New System.Drawing.Size(513,
 56)
        Me.Panel2.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Dock = System.Windows.Forms.DockStyle.Right
        Me.Button1.Font = New System.Drawing.Font(
 _
            "Microsoft Sans Serif", _
            9.0!, _
            System.Drawing.FontStyle.Regular, _
            System.Drawing.GraphicsUnit.Point, _
            CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(368,
 20)
        Me.Button1.MinimumSize = New System.Drawing.Size(0,
 20)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(62,
 20)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "&Run"
        '
        'Button2
        '
        Me.Button2.Dock = System.Windows.Forms.DockStyle.Right
        Me.Button2.Font = New System.Drawing.Font(
 _
            "Microsoft Sans Serif", _
            9.0!, _
            System.Drawing.FontStyle.Regular, _
            System.Drawing.GraphicsUnit.Point, _
            CType(0, Byte))
        Me.Button2.Location = New System.Drawing.Point(430,
 20)
        Me.Button2.MinimumSize = New System.Drawing.Size(0,
 20)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(63,
 20)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "E&xit"
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.tbxOutput)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel1.Location = New System.Drawing.Point(0,
 0)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Padding = New System.Windows.Forms.Padding(20)
        Me.Panel1.Size = New System.Drawing.Size(513,
 277)
        Me.Panel1.TabIndex = 2
        '
        'tbxOutput
        '
        Me.tbxOutput.AccessibleDescription = _
            "Displays output from application."
        Me.tbxOutput.AccessibleName = "Output
 textbox."
        Me.tbxOutput.Dock = System.Windows.Forms.DockStyle.Fill
        Me.tbxOutput.Location = New System.Drawing.Point(20,
 20)
        Me.tbxOutput.Name = "tbxOutput"
        Me.tbxOutput.Size = New System.Drawing.Size(473,
 237)
        Me.tbxOutput.TabIndex = 1
        Me.tbxOutput.Text = "Click the Run
 button to run the application."
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5,
 13)
        Me.ClientSize = New System.Drawing.Size(513,
 333)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.Panel2)
        Me.Name = "Form1"
        Me.Text = "DataProtectionPermission"
        Me.Panel2.ResumeLayout(False)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
End Class
using System;
using System.Security.Permissions;
using System.Security.Cryptography;
using System.Security;
using System.IO;

[assembly: DataProtectionPermission(
    SecurityAction.RequestMinimum, 
    Flags = DataProtectionPermissionFlags.ProtectData)]
public class DataProtect
{
    // Create a byte array for additional entropy when using the
    // Protect and Unprotect methods.
    static byte[] s_aditionalEntropy = { 9, 8, 7, 6, 5 };

    private static byte[] encryptedSecret;
    private static byte[] originalData;
    public static void Main(string[]
 args)
    {
        Console.WriteLine("Creating a permission with the Flags property ="
 +
            " ProtectData.");
        DataProtectionPermission sp = new DataProtectionPermission(
            DataProtectionPermissionFlags.ProtectData);
        // Deny the permission to protect data.
        sp.Deny();
        // The following code results in an exception due to an attempt
 to
        // protect data.
        ProtectData();
        // Remove the Deny for ProtectData permission.
        CodeAccessPermission.RevertDeny();
        // The call to protect data will now succeed.
        ProtectData();
        DataProtectionPermission sp2 = new DataProtectionPermission(
            DataProtectionPermissionFlags.UnprotectData);
        sp2.Deny();
        // The following code results in an exception due to an attempt
        // to unprotect data.
        UnprotectData();
        // Remove the Deny for UnprotectData permission.
        CodeAccessPermission.RevertDeny();

        UnprotectData();
        // Demonstrate the attribute.
        TryProtectData();
        // Demonstrate the behavior of the class members.
        ShowMembers();

        Console.WriteLine("Press the Enter key to exit.");
        Console.ReadKey();
        return;
    }

    //Deny the permission the ability to add to a store.
    [DataProtectionPermission(
         SecurityAction.Deny, 
         Flags = DataProtectionPermissionFlags.ProtectData)]
    private static void
 TryProtectData()
    {
        try
        {
            ProtectData();
            return;
        }
        catch (SecurityException e)
        {
            Console.WriteLine("A security exception thrown when attempting:"
 +
                ((DataProtectionPermission)e.DenySetInstance).Flags);
            return;
        }
    }

    // The following method is intended to demonstrate only the behavior
 of
    // DataProtectionPermission class members,and not their practical
 usage.
    // Most properties and methods in this class are used for the resolution
    // and enforcement of security policy by the security infrastructure
 code.
    private static void
 ShowMembers()
    {
        Console.WriteLine("Creating four DataProtectionPermissions");
        Console.WriteLine("Creating the first permission with the Flags "
 + 
            "property = ProtectData.");
        DataProtectionPermission sp1 = new DataProtectionPermission(
            DataProtectionPermissionFlags.ProtectData);

        Console.WriteLine("Creating the second permission with the Flags "
 + 
            "property = AllFlags.");

        DataProtectionPermission sp2 = new DataProtectionPermission(
            DataProtectionPermissionFlags.AllFlags);

        Console.WriteLine("Creating the third permission with a permission "
 +
            "state = Unrestricted.");
        DataProtectionPermission sp3 = new DataProtectionPermission(
            PermissionState.Unrestricted);
        Console.WriteLine("Creating the fourth permission with a permission"
 +
            " state = None.");

        DataProtectionPermission sp4 = new DataProtectionPermission(
            PermissionState.None);
        bool rc = sp2.IsSubsetOf(sp3);
        Console.WriteLine("Is the permission with all flags set
 (AllFlags) " +
            "a subset of \n \tthe permission with an Unrestricted " +
            "permission state? " + (rc ? "Yes" : "No"));
        rc = sp1.IsSubsetOf(sp2);
        Console.WriteLine("Is the permission with ProtectData access a "
 + 
            "subset of the permission with \n" + "\tAllFlags set?
 " + 
            (rc ? "Yes" : "No"));
        rc = sp3.IsUnrestricted();
        Console.WriteLine("Is the third permission unrestricted? " + 
            (rc ? "Yes" : "No"));
        Console.WriteLine("Copying the second permission to the fourth "
 +
            "permission.");
        sp4 = (DataProtectionPermission)sp2.Copy();
        rc = sp4.Equals(sp2);
        Console.WriteLine("Is the fourth permission equal to the second "
 +
            "permission? " + (rc ? "Yes" : "No"));
        Console.WriteLine("Creating the intersection of the second and "
 +
            "first permissions.");
        sp4 = (DataProtectionPermission)sp2.Intersect(sp1);
        Console.WriteLine("The value of the Flags property is: " + 
            sp4.Flags.ToString());
        Console.WriteLine("Creating the union of the second and first "
 +
            "permissions.");
        sp4 = (DataProtectionPermission)sp2.Union(sp1);
        Console.WriteLine("Result of the union of the second permission "
 +
            "with the first: " + sp4.Flags);
        Console.WriteLine("Using an XML round trip to reset the fourth "
 +
            "permission.");
        sp4.FromXml(sp2.ToXml());
        rc = sp4.Equals(sp2);
        Console.WriteLine("Does the XML round trip result equal the " +
            "original permission? " + (rc ? "Yes" : "No"));
    }

    // Create a simple byte array containing data to be encrypted.
    public static void ProtectData()
    {
        byte[] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };

        //Encrypt the data.
        encryptedSecret = Protect(secret);
        Console.WriteLine("The encrypted byte array is:");
        if (encryptedSecret != null)
            PrintValues(encryptedSecret);

    }

    // Decrypt the data and store in a byte array.
    public static void UnprotectData()
    {
        originalData = Unprotect(encryptedSecret);
        if (originalData != null)
        {
            Console.WriteLine("\r\nThe original data is:");
            PrintValues(originalData);
        }
    }

    // Encrypt data in the specified byte array.
    public static byte[] Protect(byte[] data)
    {
        try
        {
            // Encrypt the data using DataProtectionScope.CurrentUser.
            // The result can be decrypted only by the user who encrypted
            // the data.
            return ProtectedData.Protect(
                data, 
                s_aditionalEntropy, 
                DataProtectionScope.CurrentUser);
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("Data was not encrypted. " + 
                "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
        catch (SecurityException e)
        {
            Console.WriteLine("Insufficient permissions. " + 
                "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
    }

    // Decrypt data in the specified byte array.
    public static byte[] Unprotect(byte[] data)
    {
        try
        {
            //Decrypt the data using DataProtectionScope.CurrentUser.
            return ProtectedData.Unprotect(
                data, 
                s_aditionalEntropy, 
                DataProtectionScope.CurrentUser);
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("Data was not decrypted. " + 
                "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
        catch (SecurityException e)
        {
            Console.WriteLine("Insufficient permissions. " + 
                "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
    }

    public static void PrintValues(Byte[]
 myArr)
    {
        foreach (Byte i in myArr)
        {
            Console.Write("\t{0}", i);
        }
        Console.WriteLine();
    }


}

import System.*;
import System.Security.Permissions.*;
import System.Security.Cryptography.*;
import System.Security.*;
import System.IO.*;

/** @assembly DataProtectionPermission(SecurityAction.RequestMinimum, 
    Flags = DataProtectionPermissionFlags.ProtectData)
 */
public class DataProtect
{
    // Create a byte array for additional entropy when using the
    // Protect and Unprotect methods.
    private static ubyte s_aditionalEntropy[]
 =  { 9, 8, 7, 6, 5 };
    private static ubyte encryptedSecret[];
    private static ubyte originalData[];

    public static void main(String[]
 args)
    {
        Console.WriteLine("Creating a permission with the Flags property ="
 
            + " ProtectData.");
        DataProtectionPermission sp = new DataProtectionPermission(
            DataProtectionPermissionFlags.ProtectData);

        // Deny the permission to protect data.
        sp.Deny();
        // The following code results in an exception due to an attempt
 to
        // protect data.
        ProtectData();
        // Remove the Deny for ProtectData permission.
        CodeAccessPermission.RevertDeny();
        // The call to protect data will now succeed.
        ProtectData();
        DataProtectionPermission sp2 = new DataProtectionPermission(
            DataProtectionPermissionFlags.UnprotectData);
        sp2.Deny();
        // The following code results in an exception due to an attempt
        // to unprotect data.
        UnprotectData();
        // Remove the Deny for UnprotectData permission.
        CodeAccessPermission.RevertDeny();

        UnprotectData();
        // Demonstrate the attribute.
        TryProtectData();
        // Demonstrate the behavior of the class members.
        ShowMembers();

        Console.WriteLine("Press the Enter key to exit.");
        Console.ReadKey();
        return;
    } //main

    //Deny the permission the ability to add to a store.
    /** @attribute DataProtectionPermission(SecurityAction.Deny, 
        Flags = DataProtectionPermissionFlags.ProtectData)
     */
    private static void
 TryProtectData()
    {
        try {
            ProtectData();
            return;
        }
        catch (System.Security.SecurityException e) {
            Console.WriteLine("A security exception thrown when attempting:"
 
                + ((DataProtectionPermission)(e.get_DenySetInstance())).
                get_Flags());
            return;
        }
    } //TryProtectData

    // The following method is intended to demonstrate only the behavior
 of
    // DataProtectionPermission class members,and not their practical
 usage.
    // Most properties and methods in this class are used for the resolution
    // and enforcement of security policy by the security infrastructure
 code.
    private static void
 ShowMembers()
    {
        Console.WriteLine("Creating four DataProtectionPermissions");
        Console.WriteLine("Creating the first permission with the Flags "
 
            + "property = ProtectData.");
        DataProtectionPermission sp1 = new DataProtectionPermission(
            DataProtectionPermissionFlags.ProtectData);

        Console.WriteLine("Creating the second permission with the Flags "
 
            + "property = AllFlags.");

        DataProtectionPermission sp2 = new DataProtectionPermission(
            DataProtectionPermissionFlags.AllFlags);

        Console.WriteLine("Creating the third permission with a permission "
 
            + "state = Unrestricted.");
        DataProtectionPermission sp3 = new DataProtectionPermission(
            PermissionState.Unrestricted);

        Console.WriteLine("Creating the fourth permission with a permission"
 
            + " state = None.");

        DataProtectionPermission sp4 = new DataProtectionPermission(
            PermissionState.None);
        boolean rc = sp2.IsSubsetOf(sp3);
        Console.WriteLine("Is the permission with all flags set
 (AllFlags) " 
            + "a subset of \n \tthe permission with an Unrestricted " 
            + "permission state? " + ((rc) ? "Yes" : "No"));
        rc = sp1.IsSubsetOf(sp2);
        Console.WriteLine("Is the permission with ProtectData access a "
 
            + "subset of the permission with \n" 
            + "\tAllFlags set? " + ((rc) ? "Yes"
 : "No"));

        rc = sp3.IsUnrestricted();
        Console.WriteLine("Is the third permission unrestricted? " 
            + ((rc) ? "Yes" : "No"));

        Console.WriteLine("Copying the second permission to the fourth "
 
            + "permission.");
        sp4 = (DataProtectionPermission)(sp2.Copy());
        rc = sp4.Equals(sp2);
        Console.WriteLine("Is the fourth permission equal to the second "
 
            + "permission? " 
            + ((rc) ? "Yes" : "No"));

        Console.WriteLine("Creating the intersection of the second and "
 
            + "first permissions.");
        sp4 = (DataProtectionPermission)(sp2.Intersect(sp1));
        Console.WriteLine("The value of the Flags property is: " 
            + sp4.get_Flags().ToString());

        Console.WriteLine("Creating the union of the second and first "
 
            + "permissions.");
        sp4 = (DataProtectionPermission)(sp2.Union(sp1));
        Console.WriteLine("Result of the union of the second permission "
 
            + "with the first: " + sp4.get_Flags());

        Console.WriteLine("Using an XML round trip to reset the fourth "
 
            + "permission.");
        sp4.FromXml(sp2.ToXml());
        rc = sp4.Equals(sp2);
        Console.WriteLine("Does the XML round trip result equal the " 
            + "original permission? " 
            + ((rc) ? "Yes" : "No"));
    } //ShowMembers

    // Create a simple byte array containing data to be encrypted.
    public static void ProtectData()
    {
        ubyte secret[] =  { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
        //Encrypt the data.
        encryptedSecret = Protect(secret);
        Console.WriteLine("The encrypted byte array is:");
        if (encryptedSecret != null) {
            PrintValues(encryptedSecret);
        }
    } //ProtectData

    // Decrypt the data and store in a byte array.
    public static void UnprotectData()
    {
        originalData = Unprotect(encryptedSecret);
        if (originalData != null) {
            Console.WriteLine("\r\nThe original data is:");
            PrintValues(originalData);
        }
    } //UnprotectData

    // Encrypt data in the specified byte array.
    public static ubyte[] Protect(ubyte data[])
    {
        try {
            // Encrypt the data using DataProtectionScope.CurrentUser.
            // The result can be decrypted only by the user who encrypted
            // the data.
            return ProtectedData.Protect(data, s_aditionalEntropy
,
                DataProtectionScope.CurrentUser);
        }
        catch (CryptographicException e) {
            Console.WriteLine("Data was not encrypted. " 
                + "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
        catch (System.Security.SecurityException e) {
            Console.WriteLine("Insufficient permissions. " 
                + "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
    } //Protect

    // Decrypt data in the specified byte array.
    public static ubyte[] Unprotect(ubyte data[])
    {
        try {
            //Decrypt the data using DataProtectionScope.CurrentUser.
            return ProtectedData.Unprotect(data, s_aditionalEntropy
,
                DataProtectionScope.CurrentUser);
        }
        catch (CryptographicException e) {
            Console.WriteLine("Data was not decrypted. " 
                + "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
        catch (System.Security.SecurityException e) {
            Console.WriteLine("Insufficient permissions. " 
                + "An error has occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
    } //Unprotect

    public static void PrintValues(ubyte
 myArr[])
    {
        for (int iCtr = 0; iCtr < myArr.get_Length();
 iCtr++) {
            ubyte i = myArr[iCtr];
            Console.Write("\t{0}", System.Convert.ToString(i));
        }
        Console.WriteLine();
    } //PrintValues
} //DataProtect 
継承階層継承階層
System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.DataProtectionPermission
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataProtectionPermission メンバ
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute
その他の技術情報
アクセス許可
アクセス許可要求

DataProtectionPermission コンストラクタ (DataProtectionPermissionFlags)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

アクセス許可フラグ指定して、DataProtectionPermission クラス新しインスタンス初期化します。

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

Public Sub New ( _
    flag As DataProtectionPermissionFlags _
)
Dim flag As DataProtectionPermissionFlags

Dim instance As New DataProtectionPermission(flag)
public DataProtectionPermission (
    DataProtectionPermissionFlags flag
)
public:
DataProtectionPermission (
    DataProtectionPermissionFlags flag
)
public DataProtectionPermission (
    DataProtectionPermissionFlags flag
)
public function DataProtectionPermission (
    flag : DataProtectionPermissionFlags
)

パラメータ

flag

DataProtectionPermissionFlags 値のビットごとの組み合わせ

例外例外
例外種類条件

ArgumentException

flags が、DataProtectionPermissionFlags 値の有効な組み合わせではありません。

使用例使用例

DataProtectionPermission(DataProtectionPermissionFlags) コンストラクタ使用するコード例次に示します。このコード例は、DataProtectionPermission クラストピック取り上げているコード例一部分です。

Write("Creating a permission with the Flags property = ")
WriteLine("ProtectData.")
Dim sp As New DataProtectionPermission(
 _
    DataProtectionPermissionFlags.ProtectData)
Console.WriteLine("Creating a permission with the Flags property =" +
    " ProtectData.");
DataProtectionPermission sp = new DataProtectionPermission(
    DataProtectionPermissionFlags.ProtectData);
Console.WriteLine("Creating a permission with the Flags property =" 
    + " ProtectData.");
DataProtectionPermission sp = new DataProtectionPermission(
    DataProtectionPermissionFlags.ProtectData);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataProtectionPermission クラス
DataProtectionPermission メンバ
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute
DataProtectionPermissionFlags
その他の技術情報
アクセス許可
アクセス許可要求

DataProtectionPermission コンストラクタ (PermissionState)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

アクセス許可の状態を指定して、DataProtectionPermission クラス新しインスタンス初期化します。

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

Public Sub New ( _
    state As PermissionState _
)
Dim state As PermissionState

Dim instance As New DataProtectionPermission(state)
public DataProtectionPermission (
    PermissionState state
)
public:
DataProtectionPermission (
    PermissionState state
)
public DataProtectionPermission (
    PermissionState state
)
public function DataProtectionPermission (
    state : PermissionState
)

パラメータ

state

PermissionState 値の 1 つ

例外例外
例外種類条件

ArgumentException

state有効な PermissionState 値ではありません。

解説解説

データおよびメモリ対する完全に制限されアクセス許可 (None)、または無制限アクセス許可 (Unrestricted) を作成します

使用例使用例

DataProtectionPermission(PermissionState) コンストラクタ使用するコード例次に示します。このコード例は、DataProtectionPermission クラストピック取り上げているコード例一部分です。

Dim sp3 As New DataProtectionPermission(PermissionState.Unrestricted)
DataProtectionPermission sp3 = new DataProtectionPermission(
    PermissionState.Unrestricted);
DataProtectionPermission sp3 = new DataProtectionPermission(
    PermissionState.Unrestricted);
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataProtectionPermission クラス
DataProtectionPermission メンバ
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute
その他の技術情報
アクセス許可
アクセス許可要求

DataProtectionPermission コンストラクタ

DataProtectionPermission クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
DataProtectionPermission (DataProtectionPermissionFlags) アクセス許可フラグ指定してDataProtectionPermission クラス新しインスタンス初期化します。
DataProtectionPermission (PermissionState) アクセス許可の状態を指定してDataProtectionPermission クラス新しインスタンス初期化します。
参照参照

関連項目

DataProtectionPermission クラス
DataProtectionPermission メンバ
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute
DataProtectionPermissionFlags

その他の技術情報

アクセス許可
アクセス許可要求

DataProtectionPermission プロパティ


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

参照参照

関連項目

DataProtectionPermission クラス
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute

その他の技術情報

アクセス許可
アクセス許可要求

DataProtectionPermission メソッド


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

( プロテクト メソッド参照)
  名前 説明
パブリック メソッド Assert  アクセス許可要求によって保護されているリソースへのアクセス許可が、スタックの上位にある呼び出し元に与えられていない場合でも、呼び出しコードが、このメソッド呼び出すコード通じてリソースアクセスできるように宣言しますAssert使用すると、セキュリティ上の問題発生することがあります。 ( CodeAccessPermission から継承されます。)
パブリック メソッド Copy オーバーライドされます現在のアクセス許可コピー作成して返します
パブリック メソッド Demand  コール スタック内の上位にあるすべての呼び出し元に現在のインスタンスによって指定されているアクセス許可与えられていない場合は、実行時に SecurityException を強制します。 ( CodeAccessPermission から継承されます。)
パブリック メソッド Deny  コール スタックの上位の呼び出し元が、このメソッド呼び出すコード使用して現在のインスタンスによって指定されるリソースアクセスできないようにします。 ( CodeAccessPermission から継承されます。)
パブリック メソッド Equals  オーバーロードされます。 ( CodeAccessPermission から継承されます。)
パブリック メソッド FromXml オーバーライドされますXML エンコーディングから、特定の状態のアクセス許可再構築ます。
パブリック メソッド GetHashCode  ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用適した CodeAccessPermission オブジェクトハッシュ コード取得します。 ( CodeAccessPermission から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 ( Object から継承されます。)
パブリック メソッド Intersect オーバーライドされます現在のアクセス許可指定したアクセス許可積集合を表すアクセス許可作成して返します
パブリック メソッド IsSubsetOf オーバーライドされます現在のアクセス許可が、指定したアクセス許可サブセットかどうか判断します
パブリック メソッド IsUnrestricted 現在のアクセス許可無制限かどうかを示す値を返します
パブリック メソッド PermitOnly  コール スタックの上位の呼び出し元が、このメソッド呼び出すコード使用して現在のインスタンスによって指定されるリソース以外のすべてのリソースアクセスできないようにします。 ( CodeAccessPermission から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 ( Object から継承されます。)
パブリック メソッド RevertAll  現在のフレーム対す以前オーバーライドをすべて削除し無効にます。 ( CodeAccessPermission から継承されます。)
パブリック メソッド RevertAssert  現在のフレーム対す以前Assert をすべて削除し無効にます。 ( CodeAccessPermission から継承されます。)
パブリック メソッド RevertDeny  現在のフレーム対す以前Deny をすべて削除し無効にます。 ( CodeAccessPermission から継承されます。)
パブリック メソッド RevertPermitOnly  現在のフレーム対す以前の PermitOnly をすべて削除し無効にます。 ( CodeAccessPermission から継承されます。)
パブリック メソッド ToString  現在のアクセス許可オブジェクト文字列形式作成して返します。 ( CodeAccessPermission から継承されます。)
パブリック メソッド ToXml オーバーライドされますアクセス許可とその現在の状態を表す XML エンコーディング作成します
パブリック メソッド Union オーバーライドされます現在のアクセス許可指定したアクセス許可和集合を表すアクセス許可作成します
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataProtectionPermission クラス
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute

その他の技術情報

アクセス許可
アクセス許可要求

DataProtectionPermission メンバ

暗号化されたデータメモリアクセスする権利制御します。このクラス継承できません。

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


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド DataProtectionPermission オーバーロードされます。 DataProtectionPermission クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド参照)
  名前 説明
パブリック メソッド Assert  アクセス許可要求によって保護されているリソースへのアクセス許可が、スタックの上位にある呼び出し元に与えられていない場合でも、呼び出しコードが、このメソッド呼び出すコード通じてリソースアクセスできるように宣言しますAssert使用すると、セキュリティ上の問題発生することがあります。 (CodeAccessPermission から継承されます。)
パブリック メソッド Copy オーバーライドされます現在のアクセス許可コピー作成して返します
パブリック メソッド Demand  コール スタック内の上位にあるすべての呼び出し元に現在のインスタンスによって指定されているアクセス許可与えられていない場合は、実行時に SecurityException を強制します。 (CodeAccessPermission から継承されます。)
パブリック メソッド Deny  コール スタックの上位の呼び出し元が、このメソッド呼び出すコード使用して現在のインスタンスによって指定されるリソースアクセスできないようにします。 (CodeAccessPermission から継承されます。)
パブリック メソッド Equals  オーバーロードされます。 ( CodeAccessPermission から継承されます。)
パブリック メソッド FromXml オーバーライドされますXML エンコーディングから、特定の状態のアクセス許可再構築ます。
パブリック メソッド GetHashCode  ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用適した CodeAccessPermission オブジェクトハッシュ コード取得します。 (CodeAccessPermission から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 (Object から継承されます。)
パブリック メソッド Intersect オーバーライドされます現在のアクセス許可指定したアクセス許可積集合を表すアクセス許可作成して返します
パブリック メソッド IsSubsetOf オーバーライドされます現在のアクセス許可が、指定したアクセス許可サブセットかどうか判断します
パブリック メソッド IsUnrestricted 現在のアクセス許可無制限かどうかを示す値を返します
パブリック メソッド PermitOnly  コール スタックの上位の呼び出し元が、このメソッド呼び出すコード使用して現在のインスタンスによって指定されるリソース以外のすべてのリソースアクセスできないようにします。 (CodeAccessPermission から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 (Object から継承されます。)
パブリック メソッド RevertAll  現在のフレーム対す以前オーバーライドをすべて削除し無効にます。 (CodeAccessPermission から継承されます。)
パブリック メソッド RevertAssert  現在のフレーム対す以前Assert をすべて削除し無効にます。 (CodeAccessPermission から継承されます。)
パブリック メソッド RevertDeny  現在のフレーム対す以前Deny をすべて削除し無効にます。 (CodeAccessPermission から継承されます。)
パブリック メソッド RevertPermitOnly  現在のフレーム対す以前の PermitOnly をすべて削除し無効にます。 (CodeAccessPermission から継承されます。)
パブリック メソッド ToString  現在のアクセス許可オブジェクト文字列形式作成して返します。 (CodeAccessPermission から継承されます。)
パブリック メソッド ToXml オーバーライドされますアクセス許可とその現在の状態を表す XML エンコーディング作成します
パブリック メソッド Union オーバーライドされます現在のアクセス許可指定したアクセス許可和集合を表すアクセス許可作成します
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DataProtectionPermission クラス
System.Security.Permissions 名前空間
DataProtectionPermissionAttribute

その他の技術情報

アクセス許可
アクセス許可要求


このページでは「.NET Framework クラス ライブラリ リファレンス」からDataProtectionPermissionを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDataProtectionPermissionを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDataProtectionPermission を検索

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

辞書ショートカット

すべての辞書の索引

「DataProtectionPermission」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS