Expand
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

'Declaration

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class UIPermission _
	Inherits 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.


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



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 SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

The .NET Framework does 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: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Community ContentAdd
Page view tracker