/// <summary> /// Checks if a type belongs to a friend assembly. /// </summary> /// <param name="type">Type to check</param> /// <returns>true if type belongs to a friend assembly, false otherwise.</returns> internal static bool IsFriendClass(Type type) { lock (_lockObject) { if (ServiceIsFriend == null) { string assemblyName = string.Empty; string publicKey = string.Empty; bool hasStrongName = false; ServiceIsFriend = false; // Look for the type's Assembly name and private key in the assembly strong name // if the assembly has no StrongName it is not a friend assembly System.Collections.IEnumerator e = type.Assembly.Evidence.GetHostEnumerator(); e.Reset(); while (e.MoveNext()) { if (e.Current is System.Security.Policy.StrongName) { hasStrongName = true; System.Security.Policy.StrongName sn = e.Current as System.Security.Policy.StrongName; assemblyName = sn.Name; publicKey = sn.PublicKey.ToString(); break; } } if (hasStrongName) { // Compare the assemblyName and publicKey against the list of InternalsVisibleTo assemblies. foreach (object assembly in Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute), false)) { System.Runtime.CompilerServices.InternalsVisibleToAttribute friendClass = assembly as System.Runtime.CompilerServices.InternalsVisibleToAttribute; string[] friendAssemblyName = friendClass.AssemblyName.Split(',', '='); // friendClass.AssemblyName should match the following format: "Name, PublicKey=PublicKeyBlob" // If the assembly has no PublicKey continue, the friend class has to be signed if (friendAssemblyName.Length != 3) { continue; } if (friendAssemblyName[0].Equals(assemblyName, StringComparison.InvariantCultureIgnoreCase) && friendAssemblyName[2].Equals(publicKey, StringComparison.InvariantCultureIgnoreCase)) { ServiceIsFriend = true; break; } } } // Result gets cached in ServiceIsFriend for future requests } } return((bool)ServiceIsFriend); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string filePath = Server.MapPath("ContentFile.txt"); string pageUrl = @"http://www.microsoft.com/en/us/default.aspx?redir=true"; ReportViewer1.ProcessingMode = ProcessingMode.Local; // Create a byte array representing the public key token for MyLibrary.dll byte[] publicKey = new byte[8] { 0x8c, 0x2c, 0x20, 0x92, 0xee, 0xa1, 0xf1, 0xab }; // Create a strong name object for MyLibrary.dll. This assembly is built by the MyLibrary project and can // be found in the <solution_root>\MyLibrary\bin\Debug directory. System.Security.Policy.StrongName sn = new System.Security.Policy.StrongName( new System.Security.Permissions.StrongNamePublicKeyBlob(publicKey), "MyLibrary", new Version("1.0.0.0")); // Add MyLibrary to the list of assemblies that are trusted to execute in the sandboxed application domain ReportViewer1.LocalReport.AddFullTrustModuleInSandboxAppDomain(sn); PermissionSet permissions = new PermissionSet(PermissionState.None); //// Need Execution permission to execute expressions in the report and any referenced custom assembly. This //// is the default permission. permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); //// Need FileIOPermission for the referenced assembly to read the file permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, filePath)); //// Need WebPermission to execute Web requests permissions.AddPermission(new WebPermission(NetworkAccess.Connect, pageUrl)); // Set the permissions ReportViewer1.LocalReport.SetBasePermissionsForSandboxAppDomain(permissions); // Set the file path and the URL to be accessed by the report List <ReportParameter> parameters = new List <ReportParameter>(); parameters.Add(new ReportParameter("FilePath", filePath)); parameters.Add(new ReportParameter("PageUrl", pageUrl)); ReportViewer1.LocalReport.SetParameters(parameters); } }
public static void RunInAnotherAppDomain(string assemblyFile, string[] args) { // RazorEngine cannot clean up from the default appdomain... Console.WriteLine("Switching to second AppDomain, for RazorEngine..."); AppDomainSetup adSetup = new AppDomainSetup(); adSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var current = AppDomain.CurrentDomain; // You only need to add strongnames when your appdomain is not a full trust environment. var strongNames = new System.Security.Policy.StrongName[0]; var domain = AppDomain.CreateDomain( Path.GetFileNameWithoutExtension(assemblyFile), null, current.SetupInformation, new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted), strongNames); domain.ExecuteAssembly(assemblyFile, args); // RazorEngine will cleanup. AppDomain.Unload(domain); }
public void RemoveFullTrustAssembly(System.Security.Policy.StrongName sn) { }
/// <summary> /// Adiciona um modulo confiável para o dominio SandBox dos relatórios. /// </summary> /// <param name="strongName"></param> public static void AddFullTrustModuleInSandboxAppDomain(System.Security.Policy.StrongName strongName) { strongName.Require("strongName").NotNull(); _fullTrustModules.Add(strongName); }