ClosedXML SecurityException:不允许请求的注册表访问

问题描述 投票:0回答:5

我正在使用 ClosedXML 导出 Excel 文件,但似乎无法导出 Excel 文件。每次我单击“导出 Excel 文件 (XLSX)”按钮时,都会收到错误消息。见下文...

    using (XLWorkbook wb = new XLWorkbook())
    {
        wb.Worksheets.Add(dsInput);
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=" + sFileName + ".xlsx");
        using (MemoryStream MyMemoryStream = new MemoryStream())
        {

            wb.SaveAs(MyMemoryStream, false);

            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
    }

我收到此错误:SecurityException:不允许请求的注册表访问。

Exception thrown: 'System.TypeInitializationException' in WindowsBase.dll
System.TypeInitializationException: The type initializer for 
'MS.Utility.EventTrace' threw an exception. ---> 
System.Security.SecurityException: Requested registry access is not allowed.
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at Microsoft.Win32.Registry.GetValue(String keyName, String valueName, 
Object defaultValue)
at MS.Utility.EventTrace.IsClassicETWRegistryEnabled()
at MS.Utility.EventTrace..cctor()
--- End of inner exception stack trace ---
at MS.Utility.EventTrace.EasyTraceEvent(Keyword keywords, Event eventID)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, 
FileAccess packageAccess, Boolean streaming)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.CreateCore(Stream stream)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(Stream 
stream, SpreadsheetDocumentType type, Boolean autoSave)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(Stream 
stream, SpreadsheetDocumentType type)
at ClosedXML.Excel.XLWorkbook.CreatePackage(Stream stream, Boolean 
newStream, SpreadsheetDocumentType spreadsheetDocumentType, Boolean 
validate) in C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook_Save.cs:line 111
at ClosedXML.Excel.XLWorkbook.SaveAs(Stream stream, Boolean validate) in 
C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook.cs:line 547
at ExcelHelper.ToExcel(DataSet dsInput, String sFileName, HttpResponse 
Response) in c:\inetpub\wwwroot\Felbro_B\App_Code\ExcelHelper.cs:line 139
c# asp.net closedxml
5个回答
13
投票

我通过从 Web.Config 文件中删除identity impersonate="true" 解决了这个问题。


4
投票

对于将来遇到此问题的人:我查看了 .NET 源参考,它需要访问的注册表项是 HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics。授予“每个人”对该特定密钥的读取访问权限不会产生我能想到的安全隐患,并且可以解决问题。


0
投票

这通常是由 IIS 匿名身份验证引起的。如果您的 iis web Application Settings 启用了 aspnet:AllowAnonymousImpersonation 并启用了 IIS 身份验证 Anonymous Authenticaion & Asp.net Impersonate,您的 Web 应用程序将通过 IIS 中指定的用户访问 HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics 身份验证 -> 匿名身份验证 -> 特定用户,您应该确保该用户(IUSR)具有对注册表项的读取权限。您还可以禁用 aspnet:AllowAnonymousImpersonation 或禁用 Asp.net Impersonate

但是特定的注册表项并不总是存在,如果不存在,似乎也不会出现问题。


0
投票

不是:

HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics

HKEY_USERS\<<SID ID of Service Account Running SSRS>>\Software\Microsoft\Avalon.Graphics

...然后向执行帐户授予读取权限,该帐户在报表服务器配置管理器中设置。


0
投票

将我们的 Web 服务器从运行 IIS 8.5 的 Windows Server 2012R2 迁移到运行 IIS 10.0 的 2019 后,我遇到了这个问题。使用 OpenXML SDK 打开 Excel 文件 (.xlsx) 会导致错误,但仅限于新服务器上。我使用 SysInternals Process Monitor 确认问题是注册表项 HKEY_USERS\S-1-5-20\SOFTWARE\Microsoft\Avalon.Graphics 上的访问被拒绝错误。该 SID 是内置的 NETWORK SERVICE 帐户,我们一直使用它来支持对后端 SQL Server 的模拟和 Kerberos 委派(14 年)。我想知道我是否错过了 IIS 中应用程序池的更改,因此我进行了搜索并得出了 https://learn.microsoft.com/en-us/iis/manage/configuring-security/application- pool-identities,它描述了 IIS 7.0 中添加的 ApplicationPoolIdentity 类型(显然,我没有跟上)。更改每个应用程序池以使用该类型解决了问题。无需更改注册表权限。

© www.soinside.com 2019 - 2024. All rights reserved.