IntSecurity.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / CommonUI / System / Drawing / IntSecurity.cs / 1 / IntSecurity.cs

                            namespace System.Drawing { 
    using System;
    using System.IO;
    using System.Security;
    using System.Security.Permissions; 
    using System.Drawing.Printing;
 
    internal static class IntSecurity { 
        private static readonly UIPermission AllWindows = new UIPermission(UIPermissionWindow.AllWindows);
        private static readonly UIPermission SafeSubWindows = new UIPermission(UIPermissionWindow.SafeSubWindows); 

        public static readonly CodeAccessPermission UnmanagedCode = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);

        public static readonly CodeAccessPermission ObjectFromWin32Handle = UnmanagedCode; 
        public static readonly CodeAccessPermission Win32HandleManipulation = UnmanagedCode;
 
        public static readonly PrintingPermission NoPrinting = new PrintingPermission(PrintingPermissionLevel.NoPrinting); 
        public static readonly PrintingPermission SafePrinting = new PrintingPermission(PrintingPermissionLevel.SafePrinting);
        public static readonly PrintingPermission DefaultPrinting = new PrintingPermission(PrintingPermissionLevel.DefaultPrinting); 
        public static readonly PrintingPermission AllPrinting = new PrintingPermission(PrintingPermissionLevel.AllPrinting);

        internal static void DemandReadFileIO(string fileName) {
            string full = fileName; 

            full = UnsafeGetFullPath(fileName); 
 
            new FileIOPermission(FileIOPermissionAccess.Read, full).Demand();
        } 

        internal static void DemandWriteFileIO(string fileName) {
            string full = fileName;
 
            full = UnsafeGetFullPath(fileName);
 
            new FileIOPermission(FileIOPermissionAccess.Write, full).Demand(); 
        }
 
        internal static string UnsafeGetFullPath(string fileName) {
            string full = fileName;

            FileIOPermission fiop = new FileIOPermission(PermissionState.None); 
            fiop.AllFiles = FileIOPermissionAccess.PathDiscovery;
            fiop.Assert(); 
 
            try {
                full = Path.GetFullPath(fileName); 
            } finally {
                CodeAccessPermission.RevertAssert();
            }
 
            return full;
        } 
 
        static PermissionSet allPrintingAndUnmanagedCode;
        public static PermissionSet AllPrintingAndUnmanagedCode { 
            get {
                if (allPrintingAndUnmanagedCode == null) {
                    PermissionSet temp = new PermissionSet(PermissionState.None);
                    temp.SetPermission(IntSecurity.UnmanagedCode); 
                    temp.SetPermission(IntSecurity.AllPrinting);
                    allPrintingAndUnmanagedCode = temp; 
                } 
                return allPrintingAndUnmanagedCode;
            } 
        }

        internal static bool HasPermission(PrintingPermission permission) {
            try { 
                permission.Demand();
                return true; 
            } 
            catch (SecurityException) {
                return false; 
            }
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007. 
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK