Excel加载项安装和设置.Net安全/信任

我的环境是针对Excel 2003的VS2005 C#。当我看到在解决scheme中为我创build了一个.msi项目时,我对这个加载项的安装程序给了我一点信心。 不用说,这是整个项目中最棘手的部分,我完全停留在如何在安装过程中将Full Trust部署和设置为我的加载项。

我遵循http://msdn.microsoft.com/en-us/library/aa537179(office.11​​).aspx – “使用Windows Installer为Office解决scheme部署Visual Studio 2005工具:演练(第2部分2)“。

该文件中似乎有一些不准确的地方,但实际上是小事。 我正在使用VSTO 2005工具示例项目来将.net安全性设置为.msi的自定义操作。 这似乎工作,但目前不允许我的插件加载,除非我用.Net框架configuration工具手动更改一些东西。 安装完成后,我会看到Excel Addins的相应registry项以及0x03的相应LoadBehavior。 从“控制面板”中的“.Net 2.0 Frameworkconfiguration”小程序中,我看到有2个节点添加到我的运行时安全策略中。

运行时安全策略 – >用户 – >代码组 – > All_Code – > MyCodeGroup – > MyCodeGroupChild。

MyCodeGroup设置被设置为NONE的URLvalidation和policylevel。 MyCodeGroup子设置是STRONG NAME,并且与我为我的加载项DLL设置的内容相匹配。

在这个设置中,插件将不会加载。 如果我将“MyCodeGroup”策略从“无”更改为“完全信任”,则可以使用。 (我重置我的registry加载行为从0x02回到0x03)。

任何想法呢? 我在网上找不到很多东西来表明我哪里出错了。

担,

根据我的经验,我发现使用CASPOL.exe是设置安全策略的最简单方法。

例如,您可以使用此命令授予对文件夹中所有DLL的完全信任:caspol -u -ag All_Code -url C:\ FolderName \ FolderName * FullTrust -n“Name”-d“Description”

看看这个MSDN文章: http : //msdn.microsoft.com/en-us/library/zdc263t0.aspx

或专门在CASPOL上的这个: http : //msdn.microsoft.com/en-us/library/cb6t8dtz.aspx

或者你可以滚动自己的C#项目来做到这一点,使用此代码作为基础: http : //www.koders.com/csharp/fidDC5A2D42FE98AD20FFC65C44​​35785CDFE3CB5B10.aspx

那是我们做的。

同样根据注释:从您的msi构build中排除PIA和VSTO运行时(转到msi项目中的引用,右键单击它们并select“排除”),然后单独安装。 只需谷歌的“Excel 2003 PIA下载”和“VSTO运行时下载”来获取安装程序。 让所有的客户在你的应用程序之前安装这些程序。

这样你只需要担心你的插件的安全性。

如果您有自定义程序集,则只需要setsecurity。 你是对的示例代码不起作用,但它需要一个小的改变。 让我知道如果CaspolSecurityPolicyCreator此代码更改将修复它。 (或其他需要比较我的原件,并找出有什么区别)内部静态无效AddSecurityPolicy(BOOL machinePolicyLevel,stringsolutionCodeGroupName,stringsolutionCodeGroupDescription,stringassemblyPath,stringassemblyCodeGroupName,stringassemblyCodeGroupDescription){stringframeworkFolder = GetFrameworkFolder();

string solutionInstallationLocation = Path.GetDirectoryName(assemblyPath); string solutionInstallationUrl = Path.Combine(solutionInstallationLocation, "*"); string policyLevel; string parentCodeGroup; if (machinePolicyLevel) { policyLevel = "-m"; // Use Machine-level policy. parentCodeGroup = "My_Computer_Zone"; // Use My_Computer_Zone for assemblies installed on the computer. } else { policyLevel = "-u"; // Use User-level policy. parentCodeGroup = "All_Code"; } // Add the solution code group. Grant no permission at this level. string arguments = policyLevel + " -q -ag " + parentCodeGroup + " -url \"" + solutionInstallationUrl + "\" Nothing -n \"" + solutionCodeGroupName + "\" -d \"" + solutionCodeGroupDescription + "\""; try { RunCaspolCommand(frameworkFolder, arguments); } catch (Exception ex) { string error = String.Format("Cannot create the security code group '{0}'.", solutionCodeGroupName); throw new Exception(error, ex); } // Add the assembly code group. Grant FullTrust permissions to the main assembly. try { // Use the assembly strong name as the membership condition. // Ensure that the assembly is strong-named to give it full trust. //AssemblyName assemblyName = Assembly.LoadFile(assemblyPath).GetName(); //arguments = policyLevel + " -q -ag \"" + solutionCodeGroupName + "\" -strong -file \"" + assemblyPath + "\" \"" + assemblyName.Name + "\" \"" + assemblyName.Version.ToString(4) + "\" FullTrust -n \"" + assemblyCodeGroupName + "\" -d \"" + assemblyCodeGroupDescription + "\""; //RunCaspolCommand(frameworkFolder, arguments); //TODO- MS Hardcoded for now (better at assembly dll level use todo 1) arguments = policyLevel + " -q -ag \"" + solutionCodeGroupName + "\" -url \"" + solutionInstallationUrl + "\" FullTrust -n \"" + assemblyCodeGroupName + "\" -d \"" + assemblyCodeGroupDescription + "\""; RunCaspolCommand(frameworkFolder, arguments); //TODO: 1 code below will create a separate group per assembly path, also need to check if AcnUI assembly is installed in GAC. //AddFullTrust(frameworkFolder, assemblyPath, policyLevel, solutionCodeGroupName, assemblyCodeGroupName, assemblyCodeGroupDescription); } catch (Exception ex) { try { // Clean the solutionCodeGroupName. RemoveSecurityPolicy(machinePolicyLevel, solutionCodeGroupName); } catch {} string error = String.Format("Cannot create the security code group '{0}'.", assemblyCodeGroupName); throw new Exception(error, ex); } } internal static void RemoveSecurityPolicy( bool machinePolicyLevel, string solutionCodeGroupName) { string frameworkFolder = GetFrameworkFolder(); string policyLevel; if (machinePolicyLevel) policyLevel = "-m"; // Use Machine-level policy. else policyLevel = "-u"; // Use User-level policy. string arguments = policyLevel + " -q -rg \"" + solutionCodeGroupName + "\""; RunCaspolCommand(frameworkFolder, arguments); } private static string GetFrameworkFolder() { // Get the targeted Framework folder. Version version = new Version(2, 0, 50727); return GetRuntimeInstallationDirectory(version, true); }