Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClosedXML SecurityException: Requested registry access is not allowed

I am using ClosedXML to export an Excel file and I can't seem to export the Excel file. Every time I click on the Button to Export the Excel File(XLSX) I get an error. See Below...

    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();
        }
    }

I am getting this error: SecurityException: Requested registry access is not allowed.

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
like image 920
Arraylist Avatar asked Dec 03 '22 22:12

Arraylist


2 Answers

I solved the issue by removing identity impersonate="true" from the Web.Config file.

like image 113
Arraylist Avatar answered Dec 06 '22 10:12

Arraylist


For future people running into this: I looked through the .NET source reference, and the registry key that it needs access to is HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics. Granting "Everyone" read access on that specific key has no security implications that I can think of, and solves the problem.

like image 43
user3093028 Avatar answered Dec 06 '22 10:12

user3093028