C# Sharepoint 2010 SPImport。运行安全异常

C# Sharepoint 2010 SPImport。运行安全异常,c#,sharepoint-2010,C#,Sharepoint 2010,我想使用SPExport(工作正常)和SPImport将一个web复制到另一个位置。我正在使用SharePoint基金会2010中的应用程序页。此代码在按钮单击事件上执行 using (SPWeb web = site.OpenWeb(sourceWebUrl)) { SPExportSettings exportSettings = new SPExportSettings();

我想使用SPExport(工作正常)和SPImport将一个web复制到另一个位置。我正在使用SharePoint基金会2010中的应用程序页。此代码在按钮单击事件上执行

using (SPWeb web = site.OpenWeb(sourceWebUrl))
                    {
                        SPExportSettings exportSettings = new SPExportSettings();
                        exportSettings.FileLocation = exportPath;
                        exportSettings.BaseFileName = exportFileName;
                        exportSettings.SiteUrl = site.Url;

                        exportSettings.ExportMethod = SPExportMethodType.ExportAll;
                        exportSettings.FileCompression = true;
                        exportSettings.IncludeVersions = SPIncludeVersions.All;
                        exportSettings.IncludeSecurity = SPIncludeSecurity.All;
                        exportSettings.ExcludeDependencies = false;
                        exportSettings.ExportFrontEndFileStreams = true;
                        exportSettings.OverwriteExistingDataFile = true;

                        SPExportObject expObj = new SPExportObject();
                        expObj.IncludeDescendants = SPIncludeDescendants.All;
                        expObj.Id = web.ID;
                        expObj.Type = SPDeploymentObjectType.Web;
                        exportSettings.ExportObjects.Add(expObj);

                        SPExport export = new SPExport(exportSettings);
                        export.Run();
                    }
 using (SPWeb web = site.OpenWeb(destinationWebUrl))
                    {
                        web.AllowUnsafeUpdates = true;

                        SPImportSettings importSettings = new SPImportSettings();

                        web.FileLocation = exportPath;
                        web.BaseFileName = exportFileName;
                        web.IncludeSecurity = SPIncludeSecurity.All;
                        web.UpdateVersions = SPUpdateVersions.Overwrite;
                        web.RetainObjectIdentity = false;
                        web.SiteUrl = site.Url;
                        web.WebUrl = web.Url;
                        web.Validate();

                        SPImport import = new SPImport(importSettings);
                        import.Run();
                        web.AllowUnsafeUpdates = false;
                    }
调用SPImport.Run()时引发异常“此页面的安全验证无效。请在Web浏览器中单击“上一步”,刷新页面,然后重试操作。”

我无法找到解决此问题的方法,既不能在应用程序页面上添加FormDigest控件,也不能在目标web上允许不安全的更新

另外,从控制台应用程序运行此代码可以正常工作,但如果代码从应用程序页面运行,它将无法工作(即使具有更高的安全性)


任何帮助都将不胜感激。谢谢。

通过添加

SPUtility.ValidateFormDigest();

在第1行。

祝贺您找到了解决方案。当你有能力时,请确保将你的答案标记为“已接受”,以便其他人可以从你的成功中学习。干杯~对于那些提升特权的人,你需要在之前这样做。