2 // System.Security.SecurityException.cs
5 // Nick Drochak(ndrochak@gol.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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
;
40 namespace System
.Security
{
44 public class SecurityException
: SystemException
, _Exception
{
46 public class SecurityException
: SystemException
{
49 string permissionState
;
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
;
62 private SecurityZone _zone
;
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; }
98 public IPermission PermissionThatFailed
{
99 get { return _permfailed; }
100 set { _permfailed = value; }
103 public object PermitOnlySetInstance
{
104 get { return _permitset; }
105 set { _permitset = value; }
110 set { _url = value; }
113 public SecurityZone Zone
{
114 get { return _zone; }
115 set { _zone = value; }
119 public string PermissionState
{
120 get { return permissionState; }
122 set { permissionState = value; }
126 public Type PermissionType
{
127 get { return permissionType; }
129 set { permissionType = value; }
134 public string GrantedSet
{
135 get { return _granted; }
137 set { _granted = value; }
141 public string RefusedSet
{
142 get { return _refused; }
144 set { _refused = value; }
150 public SecurityException ()
151 : base (Locale
.GetText ("A security error has been detected."))
153 base.HResult
= unchecked ((int)0x8013150A);
156 public SecurityException (string 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
)
178 base.HResult
= unchecked ((int)0x8013150A);
179 permissionType
= type
;
182 public SecurityException (string message
, Type type
, string state
)
185 base.HResult
= unchecked ((int)0x8013150A);
186 permissionType
= type
;
187 permissionState
= state
;
190 internal SecurityException (string message
, PermissionSet granted
, PermissionSet refused
)
193 base.HResult
= unchecked ((int)0x8013150A);
194 _granted
= granted
.ToString ();
195 _refused
= refused
.ToString ();
203 SecurityException (string message
, object deny
, object permitOnly
, MethodInfo method
,
204 object demanded
, IPermission permThatFailed
)
208 _permitset
= permitOnly
;
210 _demanded
= demanded
;
211 _permfailed
= permThatFailed
;
219 SecurityException (string message
, AssemblyName assemblyName
, PermissionSet grant
,
220 PermissionSet refused
, MethodInfo method
, SecurityAction action
, object demanded
,
221 IPermission permThatFailed
, Evidence evidence
)
224 _assembly
= assemblyName
;
225 _granted
= (grant
== null) ? String
.Empty
: grant
.ToString ();
226 _refused
= (refused
== null) ? String
.Empty
: refused
.ToString ();
229 _demanded
= demanded
;
230 _permfailed
= permThatFailed
;
231 if (_permfailed
!= null)
232 permissionType
= _permfailed
.GetType ();
233 // FIXME ? evidence ?
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 ();