System.Security.Permissions ...


.NET Framework Class Library
UIPermission Class

Controls the permissions related to user interfaces and the clipboard. This class cannot be inherited.

Namespace:  System.Security.Permissions
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class UIPermission _
    Inherits CodeAccessPermission _
    Implements IUnrestrictedPermission
Visual Basic (Usage)
Dim instance As UIPermission
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class UIPermission : CodeAccessPermission, 
    IUnrestrictedPermission
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class UIPermission sealed : public CodeAccessPermission, 
    IUnrestrictedPermission
JScript
public final class UIPermission extends CodeAccessPermission implements IUnrestrictedPermission
Remarks

Drawing and user input events in windows are user interfaces.

The permission to use windows can be one of the following: unrestricted, limited to SafeTopLevelWindows, only SafeSubWindows, or no window drawing or user input event access allowed. SafeTopLevelWindows and SafeSubWindows are restricted in title and size to prevent possible spoofing by potentially harmful code.

The permission to use the clipboard can be one of the following: unrestricted, write-only, or no clipboard access allowed. The paste limitation prevents potentially harmful applications from taking data from the clipboard without the user's consent, while still allowing the cut, copy, and paste operations when initiated by the user through keyboard commands.

Examples

The following code example shows the behavior of the UIPermission class methods.

NoteNote:

The code example is intended to show the behavior of the methods, not to demonstrate their use. In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications. Generally, only the constructors are used in application code. The created instance validates or controls resource access by using inherited CodeAccessPermission methods such as Demand.

Visual Basic
Imports System
Imports System.Security
Imports System.Security.Permissions



Public Class UIPermissionDemo


    Public Shared Sub Main(ByVal args() As String)
        IsSubsetOfDemo()
        CopyDemo()
        UnionDemo()
        IntersectDemo()
        ToFromXmlDemo()

    End Sub 'Main


    ' IsSubsetOf determines whether the current permission is a subset of the specified permission.
    Private Shared Sub IsSubsetOfDemo()
        Dim uiPerm1 As New UIPermission(UIPermissionWindow.SafeTopLevelWindows)
        Dim uiPerm2 As New UIPermission(UIPermissionWindow.SafeSubWindows)
        CheckIsSubsetOfWindow(uiPerm1, uiPerm2)
        uiPerm1 = New UIPermission(UIPermissionClipboard.AllClipboard)
        uiPerm2 = New UIPermission(UIPermissionClipboard.OwnClipboard)
        CheckIsSubsetOfClipBoard(uiPerm1, uiPerm2)

    End Sub 'IsSubsetOfDemo

    Private Shared Sub CheckIsSubsetOfWindow(ByVal uiPerm1 As UIPermission, ByVal uiPerm2 As UIPermission)
        If uiPerm1.IsSubsetOf(uiPerm2) Then
            Console.WriteLine(uiPerm1.Window.ToString() + " is a subset of " + uiPerm2.Window.ToString())
        Else
            Console.WriteLine(uiPerm1.Window.ToString() + " is not a subset of " + uiPerm2.Window.ToString())
        End If

        If uiPerm2.IsSubsetOf(uiPerm1) Then
            Console.WriteLine(uiPerm2.Window.ToString() + " is a subset of " + uiPerm1.Window.ToString())
        Else
            Console.WriteLine(uiPerm2.Window.ToString() + " is not a subset of " + uiPerm1.Window.ToString())
        End If

    End Sub 'CheckIsSubsetOfWindow

    Private Shared Sub CheckIsSubsetOfClipBoard(ByVal uiPerm1 As UIPermission, ByVal uiPerm2 As UIPermission)
        If uiPerm1.IsSubsetOf(uiPerm2) Then
            Console.WriteLine(uiPerm1.Clipboard.ToString() + " is a subset of " + uiPerm2.Clipboard.ToString())
        Else
            Console.WriteLine(uiPerm1.Clipboard.ToString() + " is not a subset of " + uiPerm2.Clipboard.ToString())
        End If

        If uiPerm2.IsSubsetOf(uiPerm1) Then
            Console.WriteLine(uiPerm2.Clipboard.ToString() + " is a subset of " + uiPerm1.Clipboard.ToString())
        Else
            Console.WriteLine(uiPerm2.Clipboard.ToString() + " is not a subset of " + uiPerm1.Clipboard.ToString())
        End If

    End Sub 'CheckIsSubsetOfClipBoard

    ' Union creates a new permission that is the union of the current permission
    ' and the specified permission.
    Private Shared Sub UnionDemo()
        Dim uiPerm1 As New UIPermission(UIPermissionWindow.SafeTopLevelWindows)
        Dim uiPerm2 As New UIPermission(UIPermissionWindow.SafeSubWindows)
        Dim p3 As UIPermission = CType(uiPerm1.Union(uiPerm2), UIPermission)
        Try
            If Not (p3 Is Nothing) Then
                Console.WriteLine("The union of " + uiPerm1.Window.ToString() + " and " + vbLf + vbTab + uiPerm2.Window.ToString() + " is " + vbLf + vbTab + p3.Window.ToString() + vbLf)

            Else
                Console.WriteLine("The union of " + uiPerm1.Window.ToString() + " and " + vbLf + vbTab + uiPerm2.Window.ToString() + " is null." + vbLf)
            End If
        Catch e As SystemException
            Console.WriteLine("The union of " + uiPerm1.Window.ToString() + " and " + vbLf + vbTab + uiPerm2.Window.ToString() + " failed.")

            Console.WriteLine(e.Message)
        End Try

    End Sub 'UnionDemo

    ' Intersect creates and returns a new permission that is the intersection of the
    ' current permission and the permission specified.
    Private Shared Sub IntersectDemo()
        Dim uiPerm1 As New UIPermission(UIPermissionWindow.SafeTopLevelWindows, UIPermissionClipboard.OwnClipboard)
        Dim uiPerm2 As New UIPermission(UIPermissionWindow.SafeSubWindows, UIPermissionClipboard.NoClipboard)
        Dim p3 As UIPermission = CType(uiPerm1.Intersect(uiPerm2), UIPermission)

        Console.WriteLine("The intersection of " + uiPerm1.Window.ToString() + " and " + vbLf + vbTab + uiPerm2.Window.ToString() + " is " + p3.Window.ToString() + vbLf)
        Console.WriteLine("The intersection of " + uiPerm1.Clipboard.ToString() + " and " + vbLf + vbTab + uiPerm2.Clipboard.ToString() + " is " + p3.Clipboard.ToString() + vbLf)

    End Sub 'IntersectDemo


    'Copy creates and returns an identical copy of the current permission.
    Private Shared Sub CopyDemo()

        Dim uiPerm1 As New UIPermission(UIPermissionWindow.SafeTopLevelWindows)
        Dim uiPerm2 As New UIPermission(PermissionState.None)
        uiPerm2 = CType(uiPerm1.Copy(), UIPermission)
        If Not (uiPerm2 Is Nothing) Then
            Console.WriteLine("The copy succeeded:  " + uiPerm2.ToString() + " " + vbLf)
        End If

    End Sub 'CopyDemo

    ' ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a
    ' permission with the specified state from the XML encoding.
    Private Shared Sub ToFromXmlDemo()


        Dim uiPerm1 As New UIPermission(UIPermissionWindow.SafeTopLevelWindows)
        Dim uiPerm2 As New UIPermission(PermissionState.None)
        uiPerm2.FromXml(uiPerm1.ToXml())
        Dim result As Boolean = uiPerm2.Equals(uiPerm1)
        If result Then
            Console.WriteLine("Result of ToFromXml = " + uiPerm2.ToString())
        Else
            Console.WriteLine(uiPerm2.ToString())
            Console.WriteLine(uiPerm1.ToString())
        End If

    End Sub 'ToFromXmlDemo 
End Class 'UIPermissionDemo

C#
using System;
using System.Security;
using System.Security.Permissions;

public class UIPermissionDemo
{

    public static void Main(String[] args)
    {
        IsSubsetOfDemo();
        CopyDemo();
        UnionDemo();
        IntersectDemo();
        ToFromXmlDemo();
    }

    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private static void IsSubsetOfDemo()
    {
        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(UIPermissionWindow.SafeSubWindows);
        CheckIsSubsetOfWindow(uiPerm1, uiPerm2);
        uiPerm1 = new UIPermission(UIPermissionClipboard.AllClipboard);
        uiPerm2 = new UIPermission(UIPermissionClipboard.OwnClipboard);
        CheckIsSubsetOfClipBoard(uiPerm1, uiPerm2);
    }
    private static void CheckIsSubsetOfWindow(UIPermission uiPerm1, UIPermission uiPerm2)
    {
        if (uiPerm1.IsSubsetOf(uiPerm2))
        {
            Console.WriteLine(uiPerm1.Window.ToString() + " is a subset of " +
                uiPerm2.Window.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm1.Window.ToString() + " is not a subset of " +
                uiPerm2.Window.ToString());

        }
        if (uiPerm2.IsSubsetOf(uiPerm1))
        {
            Console.WriteLine(uiPerm2.Window.ToString() + " is a subset of " +
                uiPerm1.Window.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm2.Window.ToString() + " is not a subset of " +
                uiPerm1.Window.ToString());

        }
    }
    private static void CheckIsSubsetOfClipBoard(UIPermission uiPerm1, UIPermission uiPerm2)
    {
        if (uiPerm1.IsSubsetOf(uiPerm2))
        {
            Console.WriteLine(uiPerm1.Clipboard.ToString() + " is a subset of " +
                uiPerm2.Clipboard.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm1.Clipboard.ToString() + " is not a subset of " +
                uiPerm2.Clipboard.ToString());

        }
        if (uiPerm2.IsSubsetOf(uiPerm1))
        {
            Console.WriteLine(uiPerm2.Clipboard.ToString() + " is a subset of " +
                uiPerm1.Clipboard.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm2.Clipboard.ToString() + " is not a subset of " +
                uiPerm1.Clipboard.ToString());

        }
    }
    // Union creates a new permission that is the union of the current permission
    // and the specified permission.
    private static void UnionDemo()
    {
        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(UIPermissionWindow.SafeSubWindows);
        UIPermission p3 = (UIPermission)uiPerm1.Union(uiPerm2);
        try
        {
            if (p3 != null)
            {
                Console.WriteLine("The union of " + uiPerm1.Window.ToString() +
                    " and \n\t" + uiPerm2.Window.ToString() + " is \n\t"
                    + p3.Window.ToString() + "\n");

            }
            else
            {
                Console.WriteLine("The union of " + uiPerm1.Window.ToString() +
                    " and \n\t" + uiPerm2.Window.ToString() + " is null.\n");
            }
        }
        catch (SystemException e)
        {
            Console.WriteLine("The union of " + uiPerm1.Window.ToString() +
                    " and \n\t" + uiPerm2.Window.ToString() + " failed.");

            Console.WriteLine(e.Message);
        }

    }
    // Intersect creates and returns a new permission that is the intersection of the
    // current permission and the permission specified.
    private static void IntersectDemo()
    {
        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows, UIPermissionClipboard.OwnClipboard);
        UIPermission uiPerm2 = new UIPermission(UIPermissionWindow.SafeSubWindows, UIPermissionClipboard.NoClipboard);
        UIPermission p3 = (UIPermission)uiPerm1.Intersect(uiPerm2);

        Console.WriteLine("The intersection of " + uiPerm1.Window.ToString() + " and \n\t" +
            uiPerm2.Window.ToString() + " is " + p3.Window.ToString() + "\n");
        Console.WriteLine("The intersection of " + uiPerm1.Clipboard.ToString() + " and \n\t" +
                uiPerm2.Clipboard.ToString() + " is " + p3.Clipboard.ToString() + "\n");

    }
    //Copy creates and returns an identical copy of the current permission.
    private static void CopyDemo()
    {

        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(PermissionState.None);
        uiPerm2 = (UIPermission)uiPerm1.Copy();
        if (uiPerm2 != null)
        {
            Console.WriteLine("The copy succeeded:  " + uiPerm2.ToString() + " \n");
        }

    }
    // ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a
    // permission with the specified state from the XML encoding.
    private static void ToFromXmlDemo()
    {


        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(PermissionState.None);
        uiPerm2.FromXml(uiPerm1.ToXml());
        bool result = uiPerm2.Equals(uiPerm1);
        if (result)
        {
            Console.WriteLine("Result of ToFromXml = " + uiPerm2.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm2.ToString());
            Console.WriteLine(uiPerm1.ToString());
        }

    }

}

Visual C++
// This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods 
// of the UIPermission class.

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Runtime::InteropServices;

void IsSubsetOfDemo();          // Forward references
void CopyDemo();
void UnionDemo();
void IntersectDemo();
void ToFromXmlDemo();


int main()
{
    IsSubsetOfDemo();
    CopyDemo();
    UnionDemo();
    IntersectDemo();
    ToFromXmlDemo();
}



// IsSubsetOf determines whether the current permission is a subset of the specified permission.

void IsSubsetOfDemo()
{
    Console::WriteLine("\n**********************  IsSubsetOf() Demo **********************\n");
    UIPermission ^ uiPerm1 = gcnew UIPermission(UIPermissionWindow::SafeTopLevelWindows);
    UIPermission ^ uiPerm2 = gcnew UIPermission(UIPermissionWindow::SafeSubWindows);

    Console::WriteLine("   {0} is {1}a subset of {2} ", uiPerm1->Window,
                            uiPerm1->IsSubsetOf(uiPerm2)?"":"not ", uiPerm2->Window);

    Console::WriteLine("   {0} is {1}a subset of {2} ", uiPerm2->Window,
                            uiPerm2->IsSubsetOf(uiPerm1)?"":"not ", uiPerm1->Window);

    uiPerm1 = gcnew UIPermission(UIPermissionClipboard::AllClipboard);
    uiPerm2 = gcnew UIPermission(UIPermissionClipboard::OwnClipboard);

    Console::WriteLine("   {0} is {1}a subset of {2} ", uiPerm1->Clipboard,
                            uiPerm1->IsSubsetOf(uiPerm2)?"":"not ", uiPerm2->Clipboard);

    Console::WriteLine("   {0} is {1}a subset of {2} ", uiPerm2->Clipboard,
                            uiPerm2->IsSubsetOf(uiPerm1)?"":"not ", uiPerm1->Clipboard);
}



   // Union creates a new permission that is the union of the current permission
   // and the specified permission.
void UnionDemo()
{
    Console::WriteLine("\n************************  Union() Demo *************************\n");

    UIPermission ^ uiPerm1 = gcnew UIPermission(UIPermissionWindow::SafeTopLevelWindows);
    UIPermission ^ uiPerm2 = gcnew UIPermission(UIPermissionWindow::SafeSubWindows);

    UIPermission ^ p3 = dynamic_cast<UIPermission^>(uiPerm1->Union(uiPerm2));
    Console::WriteLine("   The union of {0} and  \n\t{1} = {2} ", uiPerm1->Window,
                               uiPerm2->Window, (nullptr != p3)?p3->Window.ToString():"null");
}

// Intersect creates and returns a new permission that is the intersection of the
// current permission and the permission specified.
void IntersectDemo()
{
    Console::WriteLine("\n**********************  Intersect() Demo ***********************\n");
    UIPermission ^ uiPerm1 = gcnew UIPermission(UIPermissionWindow::SafeTopLevelWindows,UIPermissionClipboard::OwnClipboard);
    UIPermission ^ uiPerm2 = gcnew UIPermission(UIPermissionWindow::SafeSubWindows,UIPermissionClipboard::NoClipboard);
    UIPermission ^ p3 = (UIPermission^)uiPerm1->Intersect(uiPerm2);

        Console::WriteLine("   The intersection of {0} and \n\t{1} = {2} ", uiPerm1->Window,
                               uiPerm1->Window, (nullptr != p3)?p3->Window.ToString():"null");

        Console::WriteLine("   The intersection of " + uiPerm1->Clipboard.ToString() + " and \n\t" +
                uiPerm2->Clipboard.ToString() + " is " + p3->Clipboard.ToString());
}


//Copy creates and returns an identical copy of the current permission.
void CopyDemo()
{
    Console::WriteLine("\n*************************  Copy() Demo *************************\n");

    UIPermission ^ uiPerm1 = gcnew UIPermission(UIPermissionWindow::SafeTopLevelWindows);
    UIPermission ^ uiPerm2 = gcnew UIPermission(PermissionState::None);
    uiPerm2 = (UIPermission ^)uiPerm1->Copy();
    if (uiPerm2 != nullptr)
        Console::WriteLine("The copy succeeded:  " + uiPerm2->ToString());
}


// ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a
// permission with the specified state from the XML encoding.
void ToFromXmlDemo()
{
    Console::WriteLine("\n**********************  To/From XML() Demo *********************\n");

    UIPermission ^ uiPerm1 = gcnew UIPermission(UIPermissionWindow::SafeTopLevelWindows);
    UIPermission ^ uiPerm2 = gcnew UIPermission(PermissionState::None);
    uiPerm2->FromXml(uiPerm1->ToXml());
    bool result = uiPerm2->Equals(uiPerm1);
    if (result)
        Console::WriteLine("Result of ToFromXml = " + uiPerm2->ToString());
    else
        {
        Console::WriteLine(uiPerm2->ToString());
        Console::WriteLine(uiPerm1->ToString());
        }
}
Inheritance Hierarchy

System..::.Object
  System.Security..::.CodeAccessPermission
    System.Security.Permissions..::.UIPermission
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker