static public int Main(string[] args) { object[] attrs = Assembly.GetExecutingAssembly().GetCustomAttributes(false); for (int i = 0; i < attrs.Length; i++) { if (attrs [i] is PermissionSetAttribute) { PermissionSetAttribute psa = (attrs [i] as PermissionSetAttribute); Console.WriteLine("{0} - {1}", psa.Action, psa.CreatePermissionSet()); } else if (attrs [i] is SecurityAttribute) { SecurityAttribute sa = (attrs [i] as SecurityAttribute); IPermission p = sa.CreatePermission(); PermissionSet ps = new PermissionSet(PermissionState.None); ps.AddPermission(p); Console.WriteLine("{0} - {1}", sa.Action, ps); } else { Console.WriteLine(attrs [i]); } } return(0); }
//------------------------------------------------------------ // SecurityUtil.AppendSecurityAttribute (1) // /// <summary> /// /// </summary> /// <param name="secAttrType"></param> /// <param name="positionalArgs"></param> /// <param name="argNames"></param> /// <param name="argValues"></param> /// <param name="permissionSets"></param> /// <param name="excp"></param> /// <returns></returns> //------------------------------------------------------------ public static bool AppendSecurityAttribute( Type secAttrType, Object[] positionalArgs, String[] argNames, Object[] argValues, Dictionary <SecurityAction, PermissionSet> permissionSets, out Exception excp) { excp = null; if (permissionSets == null) { return(false); } if (secAttrType == null || positionalArgs == null || positionalArgs.Length == 0) { return(false); } SecurityAction secAction; try { secAction = (SecurityAction)positionalArgs[0]; } catch (InvalidCastException ex) { excp = ex; return(false); } //switch (secAction) //{ // case SecurityAction.Assert: // case SecurityAction.Demand: // case SecurityAction.Deny: // case SecurityAction.InheritanceDemand: // case SecurityAction.LinkDemand: // case SecurityAction.PermitOnly: // case SecurityAction.RequestMinimum: // case SecurityAction.RequestOptional: // case SecurityAction.RequestRefuse: // break; // default: // return false; //} // Create the attribute instance with its positional arguments Object activatedObj = ReflectionUtil.CreateInstance( secAttrType, positionalArgs, out excp); if (activatedObj == null || excp != null) { return(false); } // Assign the named arguments to the fields or properties. int namedCount = 0; if (argNames != null && (namedCount = argNames.Length) > 0) { if (argValues == null || argValues.Length != namedCount) { return(false); } Object[] narg = new Object[1]; for (int i = 0; i < argNames.Length; ++i) { string name = argNames[i]; if (String.IsNullOrEmpty(name)) { return(false); } MemberInfo[] membs = secAttrType.GetMember(name); if (membs.Length != 1) { return(false); } narg[0] = argValues[i]; MemberTypes membType = membs[0].MemberType; BindingFlags bindFlags = 0; if (membType == MemberTypes.Property) { bindFlags = BindingFlags.SetProperty; } else if (membType == MemberTypes.Field) { bindFlags = BindingFlags.SetField; } else { return(false); } if (!ReflectionUtil.InvokeMember( secAttrType, name, bindFlags, null, activatedObj, narg, out excp)) { return(false); } } } SecurityAttribute secAttr = activatedObj as SecurityAttribute; if (secAttr == null) { return(false); } IPermission secPerm = secAttr.CreatePermission(); if (secAttr != null) { PermissionSet permSet = null; if (!permissionSets.TryGetValue(secAction, out permSet)) { permSet = new PermissionSet(null); permissionSets.Add(secAction, permSet); } else if (permSet == null) { permSet = new PermissionSet(null); permissionSets[secAction] = permSet; } DebugUtil.Assert(permSet != null); permSet.AddPermission(secPerm); return(true); } return(false); }
// TODO: add support for arrays and enums (2.0) internal static IPermission ProcessAttribute(byte[] data, ref int position) { int clen = ReadEncodedInt(data, ref position); string cnam = Encoding.UTF8.GetString(data, position, clen); position += clen; Type secattr = Type.GetType(cnam); SecurityAttribute sa = (Activator.CreateInstance(secattr, action) as SecurityAttribute); if (sa == null) { return(null); } /*int optionalParametersLength =*/ ReadEncodedInt(data, ref position); int numberOfParameters = ReadEncodedInt(data, ref position); for (int j = 0; j < numberOfParameters; j++) { bool property = false; switch (data [position++]) { case 0x53: // field (technically possible and working) property = false; break; case 0x54: // property (common case) property = true; break; default: return(null); } bool array = false; byte type = data [position++]; if (type == 0x1D) { array = true; type = data [position++]; } int plen = ReadEncodedInt(data, ref position); string pnam = Encoding.UTF8.GetString(data, position, plen); position += plen; int arrayLength = 1; if (array) { arrayLength = BitConverter.ToInt32(data, position); position += 4; } object obj = null; object[] arrayIndex = null; for (int i = 0; i < arrayLength; i++) { if (array) { // TODO - setup index (2.0) } // sadly type values doesn't match ther TypeCode enum :( switch (type) { case 0x02: // MONO_TYPE_BOOLEAN obj = (object)Convert.ToBoolean(data [position++]); break; case 0x03: // MONO_TYPE_CHAR obj = (object)Convert.ToChar(data [position]); position += 2; break; case 0x04: // MONO_TYPE_I1 obj = (object)Convert.ToSByte(data [position++]); break; case 0x05: // MONO_TYPE_U1 obj = (object)Convert.ToByte(data [position++]); break; case 0x06: // MONO_TYPE_I2 obj = (object)Convert.ToInt16(data [position]); position += 2; break; case 0x07: // MONO_TYPE_U2 obj = (object)Convert.ToUInt16(data [position]); position += 2; break; case 0x08: // MONO_TYPE_I4 obj = (object)Convert.ToInt32(data [position]); position += 4; break; case 0x09: // MONO_TYPE_U4 obj = (object)Convert.ToUInt32(data [position]); position += 4; break; case 0x0A: // MONO_TYPE_I8 obj = (object)Convert.ToInt64(data [position]); position += 8; break; case 0x0B: // MONO_TYPE_U8 obj = (object)Convert.ToUInt64(data [position]); position += 8; break; case 0x0C: // MONO_TYPE_R4 obj = (object)Convert.ToSingle(data [position]); position += 4; break; case 0x0D: // MONO_TYPE_R8 obj = (object)Convert.ToDouble(data [position]); position += 8; break; case 0x0E: // MONO_TYPE_STRING string s = null; if (data [position] != 0xFF) { int slen = ReadEncodedInt(data, ref position); s = Encoding.UTF8.GetString(data, position, slen); position += slen; } else { position++; } obj = (object)s; break; case 0x50: // special for TYPE int tlen = ReadEncodedInt(data, ref position); obj = (object)Type.GetType(Encoding.UTF8.GetString(data, position, tlen)); position += tlen; break; default: return(null); // unsupported } if (property) { PropertyInfo pi = secattr.GetProperty(pnam); pi.SetValue(sa, obj, arrayIndex); } else { FieldInfo fi = secattr.GetField(pnam); fi.SetValue(sa, obj); } } } return(sa.CreatePermission()); }
//------------------------------------------------------------ // SecurityUtil.AppendSecurityAttribute (2) // /// <summary> /// /// </summary> /// <param name="secAttrType"></param> /// <param name="positionalArgs"></param> /// <param name="argNames"></param> /// <param name="argValues"></param> /// <param name="permissionSets"></param> /// <param name="excp"></param> /// <returns></returns> //------------------------------------------------------------ public static bool AppendSecurityAttribute( Type secAttrType, Object[] positionalArgs, List <string> fieldNames, List <object> fieldValues, List <string> propertyNames, List <object> propertyValues, Dictionary <SecurityAction, PermissionSet> permissionSets, out Exception excp) { excp = null; if (permissionSets == null) { return(false); } if (secAttrType == null || positionalArgs == null || positionalArgs.Length == 0) { return(false); } int fieldCount = fieldNames != null ? fieldNames.Count : 0; if (fieldCount != (fieldValues != null ? fieldValues.Count : 0)) { return(false); } int propertyCount = propertyNames != null ? propertyNames.Count : 0; if (propertyCount != (propertyValues != null ? propertyValues.Count : 0)) { return(false); } SecurityAction secAction; try { secAction = (SecurityAction)positionalArgs[0]; } catch (InvalidCastException ex) { excp = ex; return(false); } //switch (secAction) //{ // case SecurityAction.Assert: // case SecurityAction.Demand: // case SecurityAction.Deny: // case SecurityAction.InheritanceDemand: // case SecurityAction.LinkDemand: // case SecurityAction.PermitOnly: // case SecurityAction.RequestMinimum: // case SecurityAction.RequestOptional: // case SecurityAction.RequestRefuse: // break; // default: // return false; //} // Create the attribute instance with its positional arguments Object activatedObj = ReflectionUtil.CreateInstance( secAttrType, positionalArgs, out excp); if (activatedObj == null || excp != null) { return(false); } // Assign the named arguments to the fields. Object[] narg = new Object[1]; if (fieldCount > 0) { for (int i = 0; i < fieldCount; ++i) { narg[0] = fieldValues[i]; if (!ReflectionUtil.InvokeMember( secAttrType, fieldNames[i], BindingFlags.SetField, null, activatedObj, narg, out excp)) { return(false); } } } // Assign the named arguments to the properties. if (propertyCount > 0) { for (int i = 0; i < propertyCount; ++i) { narg[0] = propertyValues[i]; if (!ReflectionUtil.InvokeMember( secAttrType, propertyNames[i], BindingFlags.SetProperty, null, activatedObj, narg, out excp)) { return(false); } } } SecurityAttribute secAttr = activatedObj as SecurityAttribute; if (secAttr == null) { return(false); } IPermission secPerm = secAttr.CreatePermission(); if (secAttr != null) { PermissionSet permSet = null; if (!permissionSets.TryGetValue(secAction, out permSet)) { permSet = new PermissionSet(null); permissionSets.Add(secAction, permSet); } else if (permSet == null) { permSet = new PermissionSet(null); permissionSets[secAction] = permSet; } DebugUtil.Assert(permSet != null); permSet.AddPermission(secPerm); return(true); } return(false); }