4

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
Arraylist
  • 307
  • 3
  • 13

4 Answers4

13

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

Arraylist
  • 307
  • 3
  • 13
4

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.

  • 1
    This was helpful, but we first needed to reboot the machine. Maybe Windows or IIS has some sort of weird cache for these kinds of things. – RobSiklos May 05 '20 at 14:18
  • This worked for me as well! If you using ApplicationPoolIdentity, then you can grant access to only IIS APPPOOL\ and don't forget to reboot windows – Coder Dev Nov 14 '21 at 06:56
  • Just lost hours on this. In order for this to work, you have to login as user with which you are running your application. Then you must open regedit and sort permissions on a key. It would seem that during impersonation application still tries to access HKCU of a user that started the application and not the user that we are impersonating. Hence the issue. – Vladimir Kocjancic Apr 15 '22 at 12:55
0

This is usually caused by IIS anonymous authentication. If your iis web Application Settings enabled aspnet:AllowAnonymousImpersonation and IIS authentications enabled Anonymous Authenticaion & Asp.net Impersonate, your web app will access HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics by user specified in IIS Authentications -> Anonymous Authentication -> Specific User, you should make sure this user(IUSR) has read permission to the registry key. You can also disable aspnet:AllowAnonymousImpersonation or disable Asp.net Impersonate.

But the specific registry key is not always exist, if it is not exist, it seems the problem will not occurr too.

YonF
  • 641
  • 5
  • 20
0

It's not:

HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics

it's

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

…and then give READ permissions to the Execution Account, which is setup in the Report Server Configuration Manager.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Matt l
  • 1