**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security / SecurityException.cs
blob45ba804dbfb94249165f466767e606b3785954b1
1 //
2 // System.Security.SecurityException.cs
3 //
4 // Authors:
5 // Nick Drochak(ndrochak@gol.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) Nick Drochak
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38 using System.Text;
40 namespace System.Security {
42 [Serializable]
43 #if NET_2_0
44 public class SecurityException : SystemException, _Exception {
45 #else
46 public class SecurityException : SystemException {
47 #endif
48 // Fields
49 string permissionState;
50 Type permissionType;
51 private string _granted;
52 private string _refused;
53 private SecurityAction _action;
54 private object _demanded;
55 private object _denyset;
56 private object _permitset;
57 private AssemblyName _assembly;
58 private IPermission _firstperm;
59 private IPermission _permfailed;
60 private MethodInfo _method;
61 private string _url;
62 private SecurityZone _zone;
64 // Properties
66 #if NET_2_0
67 public SecurityAction Action {
68 get { return _action; }
69 set { _action = value; }
72 public object Demanded {
73 get { return _demanded; }
74 set { _demanded = value; }
77 public object DenySetInstance {
78 get { return _denyset; }
79 set { _denyset = value; }
82 public AssemblyName FailedAssemblyInfo {
83 get { return _assembly; }
84 set { _assembly = value; }
87 public IPermission FirstPermissionThatFailed {
88 get { return _firstperm; }
89 set { _firstperm = value; }
92 public MethodInfo Method {
93 get { return _method; }
94 set { _method = value; }
97 [Obsolete]
98 public IPermission PermissionThatFailed {
99 get { return _permfailed; }
100 set { _permfailed = value; }
103 public object PermitOnlySetInstance {
104 get { return _permitset; }
105 set { _permitset = value; }
108 public string Url {
109 get { return _url; }
110 set { _url = value; }
113 public SecurityZone Zone {
114 get { return _zone; }
115 set { _zone = value; }
117 #endif
119 public string PermissionState {
120 get { return permissionState; }
121 #if NET_2_0
122 set { permissionState = value; }
123 #endif
126 public Type PermissionType {
127 get { return permissionType; }
128 #if NET_2_0
129 set { permissionType = value; }
130 #endif
133 #if NET_1_1
134 public string GrantedSet {
135 get { return _granted; }
136 #if NET_2_0
137 set { _granted = value; }
138 #endif
141 public string RefusedSet {
142 get { return _refused; }
143 #if NET_2_0
144 set { _refused = value; }
145 #endif
147 #endif
148 // Constructors
150 public SecurityException ()
151 : base (Locale.GetText ("A security error has been detected."))
153 base.HResult = unchecked ((int)0x8013150A);
156 public SecurityException (string message)
157 : base (message)
159 base.HResult = unchecked ((int)0x8013150A);
162 protected SecurityException (SerializationInfo info, StreamingContext context)
163 : base (info, context)
165 base.HResult = unchecked ((int)0x8013150A);
166 permissionState = info.GetString ("PermissionState");
169 public SecurityException (string message, Exception inner)
170 : base (message, inner)
172 base.HResult = unchecked ((int)0x8013150A);
175 public SecurityException (string message, Type type)
176 : base (message)
178 base.HResult = unchecked ((int)0x8013150A);
179 permissionType = type;
182 public SecurityException (string message, Type type, string state)
183 : base (message)
185 base.HResult = unchecked ((int)0x8013150A);
186 permissionType = type;
187 permissionState = state;
190 internal SecurityException (string message, PermissionSet granted, PermissionSet refused)
191 : base (message)
193 base.HResult = unchecked ((int)0x8013150A);
194 _granted = granted.ToString ();
195 _refused = refused.ToString ();
198 #if NET_2_0
199 public
200 #else
201 internal
202 #endif
203 SecurityException (string message, object deny, object permitOnly, MethodInfo method,
204 object demanded, IPermission permThatFailed)
205 : base (message)
207 _denyset = deny;
208 _permitset = permitOnly;
209 _method = method;
210 _demanded = demanded;
211 _permfailed = permThatFailed;
214 #if NET_2_0
215 public
216 #else
217 internal
218 #endif
219 SecurityException (string message, AssemblyName assemblyName, PermissionSet grant,
220 PermissionSet refused, MethodInfo method, SecurityAction action, object demanded,
221 IPermission permThatFailed, Evidence evidence)
222 : base (message)
224 _assembly = assemblyName;
225 _granted = (grant == null) ? String.Empty : grant.ToString ();
226 _refused = (refused == null) ? String.Empty : refused.ToString ();
227 _method = method;
228 _action = action;
229 _demanded = demanded;
230 _permfailed = permThatFailed;
231 if (_permfailed != null)
232 permissionType = _permfailed.GetType ();
233 // FIXME ? evidence ?
236 // Methods
237 public override void GetObjectData (SerializationInfo info, StreamingContext context)
239 base.GetObjectData (info, context);
240 info.AddValue ("PermissionState", permissionState);
243 public override string ToString ()
245 StringBuilder sb = new StringBuilder (base.ToString ());
246 if (permissionState != null) {
247 sb.AppendFormat ("{0}State: {1}", Environment.NewLine, permissionState);
249 if (permissionType != null) {
250 sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, permissionType);
252 if ((_granted != null) && (_granted.Length > 0)) {
253 sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, _granted);
255 if ((_refused != null) && (_refused.Length > 0)) {
256 sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, _refused);
258 if (_demanded != null) {
259 sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, _demanded);
261 if (_permfailed != null) {
262 sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, _permfailed);
264 return sb.ToString ();