PermissionSet クラス
複数の異なる種類のアクセス許可を格納できるコレクションを表します。
この型のすべてのメンバの一覧については、PermissionSet メンバ を参照してください。
System.Object
System.Security.PermissionSet
System.Security.NamedPermissionSet
<Serializable>
Public Class PermissionSet Implements ISecurityEncodable, ICollection, IEnumerable, _ IStackWalk, IDeserializationCallback
[C#]
[Serializable]
public class PermissionSet : ISecurityEncodable, ICollection, IEnumerable, IStackWalk, IDeserializationCallback
[C++]
[Serializable]
public __gc class PermissionSet : public ISecurityEncodable, ICollection, IEnumerable, IStackWalk, IDeserializationCallback
[JScript]
public
Serializable
class PermissionSet implements ISecurityEncodable, ICollection, IEnumerable, IStackWalk, IDeserializationCallback
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
PermissionSet を使用すると、複数の異なるアクセス許可を 1 つのグループとして操作を実行できます。
使用例
' This sample demonstrates the use of the PermissionSet class.
Imports System
Imports System.Reflection
Imports System.Security.Permissions
Imports System.Security
Imports System.IO
Imports System.Collections
Imports Microsoft.VisualBasic
Class [MyClass]
Public Shared Sub PermissionSetDemo()
Console.WriteLine("Executing PermissionSetDemo")
Try
' Open a new PermissionSet.
Dim ps1 As New PermissionSet(PermissionState.None)
Console.WriteLine("Adding permission to open a file from a file dialog box.")
' Add a permission to the permission set.
ps1.AddPermission(New FileDialogPermission(FileDialogPermissionAccess.Open))
Console.WriteLine("Demanding permission to open a file.")
ps1.Demand()
Console.WriteLine("Demand succeeded.")
Console.WriteLine("Adding permission to save a file from a file dialog box.")
ps1.AddPermission(New FileDialogPermission(FileDialogPermissionAccess.Save))
Console.WriteLine("Demanding permission to open and save a file.")
ps1.Demand()
Console.WriteLine("Demand succeeded.")
Console.WriteLine("Adding permission to read environment variable USERNAME.")
ps1.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME"))
ps1.Demand()
Console.WriteLine("Demand succeeded.")
Console.WriteLine("Adding permission to read environment variable COMPUTERNAME.")
ps1.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Write, "COMPUTERNAME"))
' Demand all the permissions in the set.
Console.WriteLine("Demand all permissions.")
ps1.Demand()
Console.WriteLine("Demand succeeded.")
' Display the number of permissions in the set.
Console.WriteLine("Number of permissions = " & ps1.Count)
' Display the value of the IsSynchronized property.
Console.WriteLine("IsSynchronized property = " & ps1.IsSynchronized)
' Display the value of the IsReadOnly property.
Console.WriteLine("IsReadOnly property = " & ps1.IsReadOnly)
' Display the value of the SyncRoot property.
Console.WriteLine("SyncRoot property = " & CType(ps1.SyncRoot, PermissionSet).ToString())
' Display the result of a call to the ContainsNonCodeAccessPermissions method.
' Gets a value indicating whether the PermissionSet contains permissions
' that are not derived from CodeAccessPermission.
' Returns true if the PermissionSet contains permissions that are not
' derived from CodeAccessPermission; otherwise, false.
Console.WriteLine("ContainsNonCodeAccessPermissions method returned " & ps1.ContainsNonCodeAccessPermissions())
Console.WriteLine("Value of the permission set ToString = " & ControlChars.Lf & ps1.ToString())
Dim ps2 As New PermissionSet(PermissionState.None)
' Create a second permission set and compare it to the first permission set.
ps2.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME"))
ps2.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.Write, "COMPUTERNAME"))
Console.WriteLine("Second permission IsSubsetOf first permission = " & ps2.IsSubsetOf(ps1))
' Display the intersection of two permission sets.
Dim ps3 As PermissionSet = ps2.Intersect(ps1)
Console.WriteLine("The intersection of the first permission set and " & "the second permission set = " & ps3.ToString())
' Create a new permission set.
Dim ps4 As New PermissionSet(PermissionState.None)
ps4.AddPermission(New FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath("SomeFile")))
ps4.AddPermission(New FileIOPermission(FileIOPermissionAccess.Read Or FileIOPermissionAccess.Write Or FileIOPermissionAccess.Append, Path.GetFullPath("SomeFile")))
' Display the union of two permission sets.
Dim ps5 As PermissionSet = ps3.Union(ps4)
Console.WriteLine("The union of permission set 3 and permission set 4 = " & ps5.ToString())
' Remove FileIOPermission from the permission set.
ps5.RemovePermission(GetType(FileIOPermission))
Console.WriteLine("The last permission set after removing FileIOPermission = " & ps5.ToString())
' Change the permission set using SetPermission.
ps5.SetPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "USERNAME"))
Console.WriteLine("Permission set after SetPermission = " & ps5.ToString())
' Display result of ToXml and FromXml operations.
Dim ps6 As New PermissionSet(PermissionState.None)
ps6.FromXml(ps5.ToXml())
Console.WriteLine("Result of ToFromXml = " & ps6.ToString() & ControlChars.Lf)
' Display results of PermissionSet.GetEnumerator.
Dim psEnumerator As IEnumerator = ps1.GetEnumerator()
While psEnumerator.MoveNext()
Console.WriteLine(psEnumerator.Current)
End While
' Check for an unrestricted permission set.
Dim ps7 As New PermissionSet(PermissionState.Unrestricted)
Console.WriteLine("Permission set is unrestricted = " & ps7.IsUnrestricted())
' Create and display a copy of a permission set.
ps7 = ps5.Copy()
Console.WriteLine("Result of copy = " & ps7.ToString())
Catch e As Exception
Console.WriteLine(e.Message.ToString())
End Try
End Sub 'PermissionSetDemo
Overloads Shared Sub Main(ByVal args() As String)
PermissionSetDemo()
End Sub 'Main
End Class '[MyClass]
[C#]
// This sample demonstrates the use of the PermissionSet class.
using System;
using System.Reflection;
using System.Security.Permissions;
using System.Security;
using System.IO;
using System.Collections;
class MyClass
{
public static void PermissionSetDemo()
{
Console.WriteLine("Executing PermissionSetDemo");
try
{
// Open a new PermissionSet.
PermissionSet ps1 = new PermissionSet(PermissionState.None);
Console.WriteLine("Adding permission to open a file from a file dialog box.");
// Add a permission to the permission set.
ps1.AddPermission(
new FileDialogPermission(FileDialogPermissionAccess.Open));
Console.WriteLine("Demanding permission to open a file.");
ps1.Demand();
Console.WriteLine("Demand succeeded.");
Console.WriteLine("Adding permission to save a file from a file dialog box.");
ps1.AddPermission(
new FileDialogPermission(FileDialogPermissionAccess.Save));
Console.WriteLine("Demanding permission to open and save a file.");
ps1.Demand();
Console.WriteLine("Demand succeeded.");
Console.WriteLine("Adding permission to read environment variable USERNAME.");
ps1.AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME"));
ps1.Demand();
Console.WriteLine("Demand succeeded.");
Console.WriteLine("Adding permission to read environment variable COMPUTERNAME.");
ps1.AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess.Write,
"COMPUTERNAME"));
// Demand all the permissions in the set.
Console.WriteLine("Demand all permissions.");
ps1.Demand();
Console.WriteLine("Demand succeeded.");
// Display the number of permissions in the set.
Console.WriteLine("Number of permissions = " + ps1.Count);
// Display the value of the IsSynchronized property.
Console.WriteLine("IsSynchronized property = " + ps1.IsSynchronized);
// Display the value of the IsReadOnly property.
Console.WriteLine("IsReadOnly property = " + ps1.IsReadOnly);
// Display the value of the SyncRoot property.
Console.WriteLine("SyncRoot property = " + ps1.SyncRoot);
// Display the result of a call to the ContainsNonCodeAccessPermissions method.
// Gets a value indicating whether the PermissionSet contains permissions
// that are not derived from CodeAccessPermission.
// Returns true if the PermissionSet contains permissions that are not
// derived from CodeAccessPermission; otherwise, false.
Console.WriteLine("ContainsNonCodeAccessPermissions method returned " +
ps1.ContainsNonCodeAccessPermissions());
Console.WriteLine("Value of the permission set ToString = \n" + ps1.ToString());
PermissionSet ps2 = new PermissionSet(PermissionState.None);
// Create a second permission set and compare it to the first permission set.
ps2.AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME"));
ps2.AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess.Write, "COMPUTERNAME"));
Console.WriteLine("Second permission IsSubsetOf first permission = " + ps2.IsSubsetOf(ps1));
// Display the intersection of two permission sets.
PermissionSet ps3 = ps2.Intersect(ps1);
Console.WriteLine("The intersection of the first permission set and "
+ "the second permission set = " + ps3.ToString());
// Create a new permission set.
PermissionSet ps4 = new PermissionSet(PermissionState.None);
ps4.AddPermission(
new FileIOPermission(FileIOPermissionAccess.Read ,
Path.GetFullPath("SomeFile")));
ps4.AddPermission(
new FileIOPermission(FileIOPermissionAccess.Read |
FileIOPermissionAccess.Write | FileIOPermissionAccess.Append,
Path.GetFullPath("SomeFile")));
// Display the union of two permission sets.
PermissionSet ps5 = ps3.Union(ps4);
Console.WriteLine("The union of permission set 3 and permission set 4 = "
+ ps5.ToString());
// Remove FileIOPermission from the permission set.
ps5.RemovePermission(typeof(FileIOPermission));
Console.WriteLine("The last permission set after removing FileIOPermission = "
+ ps5.ToString());
// Change the permission set using SetPermission.
ps5.SetPermission(new EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "USERNAME"));
Console.WriteLine("Permission set after SetPermission = " + ps5.ToString());
// Display result of ToXml and FromXml operations.
PermissionSet ps6 = new PermissionSet(PermissionState.None);
ps6.FromXml(ps5.ToXml());
Console.WriteLine("Result of ToFromXml = " +ps6.ToString() + "\n");
// Display results of PermissionSet.GetEnumerator.
IEnumerator psEnumerator = ps1.GetEnumerator();
while (psEnumerator.MoveNext())
{
Console.WriteLine(psEnumerator.Current);
}
// Check for an unrestricted permission set.
PermissionSet ps7 = new PermissionSet(PermissionState.Unrestricted);
Console.WriteLine("Permission set is unrestricted = " + ps7.IsUnrestricted());
// Create and display a copy of a permission set.
ps7 = ps5.Copy();
Console.WriteLine("Result of copy = " + ps7.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
static void Main(string[] args)
{
PermissionSetDemo();
}
}
[C++]
// This sample demonstrates the use of the PermissionSet class.
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;
using namespace System::Security;
using namespace System::IO;
using namespace System::Collections;
void PermissionSetDemo() {
Console::WriteLine(S"Executing PermissionSetDemo");
try {
// Open a new PermissionSet.
PermissionSet* ps1 = new PermissionSet(PermissionState::None);
Console::WriteLine(S"Adding permission to open a file from a file dialog box.");
// Add a permission to the permission set.
ps1->AddPermission(new FileDialogPermission(FileDialogPermissionAccess::Open));
Console::WriteLine(S"Demanding permission to open a file.");
ps1->Demand();
Console::WriteLine(S"Demand succeeded.");
Console::WriteLine(S"Adding permission to save a file from a file dialog box.");
ps1->AddPermission(new FileDialogPermission(FileDialogPermissionAccess::Save));
Console::WriteLine(S"Demanding permission to open and save a file.");
ps1->Demand();
Console::WriteLine(S"Demand succeeded.");
Console::WriteLine(S"Adding permission to read environment variable USERNAME.");
ps1->AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess::Read, S"USERNAME"));
ps1->Demand();
Console::WriteLine(S"Demand succeeded.");
Console::WriteLine(S"Adding permission to read environment variable COMPUTERNAME.");
ps1->AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess::Write, S"COMPUTERNAME"));
// Demand all the permissions in the set.
Console::WriteLine(S"Demand all permissions.");
ps1->Demand();
Console::WriteLine(S"Demand succeeded.");
// Display the number of permissions in the set.
Console::WriteLine(S"Number of permissions = {0}", __box(ps1->Count));
// Display the value of the IsSynchronized property.
Console::WriteLine(S"IsSynchronized property = {0}", __box(ps1->IsSynchronized));
// Display the value of the IsReadOnly property.
Console::WriteLine(S"IsReadOnly property = {0}", __box(ps1->IsReadOnly));
// Display the value of the SyncRoot property.
Console::WriteLine(S"SyncRoot property = {0}", ps1->SyncRoot);
// Display the result of a call to the ContainsNonCodeAccessPermissions method.
// Gets a value indicating whether the PermissionSet contains permissions
// that are not derived from CodeAccessPermission.
// Returns true if the PermissionSet contains permissions that are not
// derived from CodeAccessPermission; otherwise, false.
Console::WriteLine(S"ContainsNonCodeAccessPermissions method returned {0}",
__box(ps1->ContainsNonCodeAccessPermissions()));
Console::WriteLine(S"Value of the permission set ToString = \n{0}", ps1->ToString());
PermissionSet* ps2 = new PermissionSet(PermissionState::None);
// Create a second permission set and compare it to the first permission set.
ps2->AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess::Read, S"USERNAME"));
ps2->AddPermission(
new EnvironmentPermission(EnvironmentPermissionAccess::Write, S"COMPUTERNAME"));
Console::WriteLine(S"Second permission IsSubsetOf first permission = {0}",
__box(ps2->IsSubsetOf(ps1)));
// Display the intersection of two permission sets.
PermissionSet* ps3 = ps2->Intersect(ps1);
Console::WriteLine(
S"The intersection of the first permission set and the second permission set = {0}",
ps3);
// Create a new permission set.
PermissionSet* ps4 = new PermissionSet(PermissionState::None);
ps4->AddPermission(new FileIOPermission(FileIOPermissionAccess::Read ,
Path::GetFullPath(S"SomeFile")));
ps4->AddPermission(
new FileIOPermission(static_cast<FileIOPermissionAccess>(
FileIOPermissionAccess::Read |
FileIOPermissionAccess::Write |
FileIOPermissionAccess::Append),
Path::GetFullPath(S"SomeFile")));
// Display the union of two permission sets.
PermissionSet* ps5 = ps3->Union(ps4);
Console::WriteLine(S"The union of permission set 3 and permission set 4 = {0}",
ps5);
// Remove FileIOPermission from the permission set.
ps5->RemovePermission(__typeof(FileIOPermission));
Console::WriteLine(S"The last permission set after removing FileIOPermission = {0}",
ps5);
// Change the permission set using SetPermission.
ps5->SetPermission(
new EnvironmentPermission(EnvironmentPermissionAccess::AllAccess, S"USERNAME"));
Console::WriteLine(S"Permission set after SetPermission = {0}", ps5);
// Display result of ToXml and FromXml operations.
PermissionSet* ps6 = new PermissionSet(PermissionState::None);
ps6->FromXml(ps5->ToXml());
Console::WriteLine(S"Result of ToFromXml = {0}\n", ps6);
// Display results of PermissionSet::GetEnumerator.
IEnumerator* psEnumerator = ps1->GetEnumerator();
while (psEnumerator->MoveNext()) {
Console::WriteLine(psEnumerator->Current);
}
// Check for an unrestricted permission set.
PermissionSet* ps7 = new PermissionSet(PermissionState::Unrestricted);
Console::WriteLine(S"Permission set is unrestricted = {0}",
__box(ps7->IsUnrestricted()));
// Create and display a copy of a permission set.
ps7 = ps5->Copy();
Console::WriteLine(S"Result of copy = {0}", ps7);
} catch (Exception* e) {
Console::WriteLine(e->Message);
}
}
int main() {
PermissionSetDemo();
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Security
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
アセンブリ: Mscorlib (Mscorlib.dll 内)
.NET Framework セキュリティ:
- StrongNameIdentityPermission (PermissionSet から継承するために必要なアクセス許可。Microsoft StrongName で署名されたアセンブリだけが PermissionSet から継承できます。)