markus / SmartUpdate / IntSecurity.cs @ f19ebe30
이력 | 보기 | 이력해설 | 다운로드 (3.49 KB)
1 |
namespace System.Drawing |
---|---|
2 |
{ |
3 |
using System; |
4 |
using System.IO; |
5 |
using System.Security; |
6 |
using System.Security.Permissions; |
7 |
using System.Drawing.Printing; |
8 |
using System.Runtime.Versioning; |
9 |
|
10 |
internal static class IntSecurity |
11 |
{ |
12 |
private static readonly UIPermission AllWindows = new UIPermission(UIPermissionWindow.AllWindows); |
13 |
private static readonly UIPermission SafeSubWindows = new UIPermission(UIPermissionWindow.SafeSubWindows); |
14 |
|
15 |
public static readonly CodeAccessPermission UnmanagedCode = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); |
16 |
|
17 |
public static readonly CodeAccessPermission ObjectFromWin32Handle = UnmanagedCode; |
18 |
public static readonly CodeAccessPermission Win32HandleManipulation = UnmanagedCode; |
19 |
|
20 |
public static readonly PrintingPermission NoPrinting = new PrintingPermission(PrintingPermissionLevel.NoPrinting); |
21 |
public static readonly PrintingPermission SafePrinting = new PrintingPermission(PrintingPermissionLevel.SafePrinting); |
22 |
public static readonly PrintingPermission DefaultPrinting = new PrintingPermission(PrintingPermissionLevel.DefaultPrinting); |
23 |
public static readonly PrintingPermission AllPrinting = new PrintingPermission(PrintingPermissionLevel.AllPrinting); |
24 |
|
25 |
[ResourceExposure(ResourceScope.None)] |
26 |
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)] |
27 |
internal static void DemandReadFileIO(string fileName) |
28 |
{ |
29 |
string full = fileName; |
30 |
|
31 |
full = UnsafeGetFullPath(fileName); |
32 |
|
33 |
new FileIOPermission(FileIOPermissionAccess.Read, full).Demand(); |
34 |
} |
35 |
|
36 |
[ResourceExposure(ResourceScope.None)] |
37 |
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)] |
38 |
internal static void DemandWriteFileIO(string fileName) |
39 |
{ |
40 |
string full = fileName; |
41 |
|
42 |
full = UnsafeGetFullPath(fileName); |
43 |
|
44 |
new FileIOPermission(FileIOPermissionAccess.Write, full).Demand(); |
45 |
} |
46 |
|
47 |
[ResourceExposure(ResourceScope.Machine)] |
48 |
[ResourceConsumption(ResourceScope.Machine)] |
49 |
internal static string UnsafeGetFullPath(string fileName) |
50 |
{ |
51 |
string full = fileName; |
52 |
|
53 |
FileIOPermission fiop = new FileIOPermission(PermissionState.None); |
54 |
fiop.AllFiles = FileIOPermissionAccess.PathDiscovery; |
55 |
fiop.Assert(); |
56 |
|
57 |
try |
58 |
{ |
59 |
full = Path.GetFullPath(fileName); |
60 |
} |
61 |
finally |
62 |
{ |
63 |
CodeAccessPermission.RevertAssert(); |
64 |
} |
65 |
|
66 |
return full; |
67 |
} |
68 |
|
69 |
static PermissionSet allPrintingAndUnmanagedCode; |
70 |
public static PermissionSet AllPrintingAndUnmanagedCode |
71 |
{ |
72 |
get |
73 |
{ |
74 |
if (allPrintingAndUnmanagedCode == null) |
75 |
{ |
76 |
PermissionSet temp = new PermissionSet(PermissionState.None); |
77 |
temp.SetPermission(IntSecurity.UnmanagedCode); |
78 |
temp.SetPermission(IntSecurity.AllPrinting); |
79 |
allPrintingAndUnmanagedCode = temp; |
80 |
} |
81 |
return allPrintingAndUnmanagedCode; |
82 |
} |
83 |
} |
84 |
|
85 |
internal static bool HasPermission(PrintingPermission permission) |
86 |
{ |
87 |
try |
88 |
{ |
89 |
permission.Demand(); |
90 |
return true; |
91 |
} |
92 |
catch (SecurityException) |
93 |
{ |
94 |
return false; |
95 |
} |
96 |
} |
97 |
} |
98 |
} |