Why am I getting “The magic number in GZip header is not correct.” error using OWIN auth against Azure SQL

送分小仙女□ 提交于 2020-01-14 09:39:30

问题


Nothing on Google or SO relates to this specific problem, so asking a new question. I created a brand new Asp.Net MVC Web Application with the standard user-security option. I also created an empty database in Azure.

I did nothing but change the default connection string to this:

  <connectionStrings>
    <add name="DefaultConnection" 
         connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

and the default connection factory to this:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

On attempting to register (when I would expect it to normally create the AspNetUsers and related tables) I get the following error:

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

Source Error:

Line 153: { Line 154: var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; Line 155: var result = await UserManager.CreateAsync(user, model.Password); Line 156: if (result.Succeeded) Line 157: {

What has any of this to do with GZip and what is causing this error? This has stopped me getting OWIN working with my Azure database for several days now.


回答1:


So here's my "theory". I think the Sql Azure Db is not setup correctly or the connection string is in error.

We know that when AspIdentity attempts to hit the database on Login and finds the tables are not set up it will try to create them.

I think there is a breakdown/bug in the AspIdentity code that under this circumstance swallows the exception generated from the bad connection string and thus the failed setup and continues on it's merry way trying to provide an authentication ticket to the client.

If we look into the code for Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer we find some GZip action:

 public virtual byte[] Serialize(AuthenticationTicket model)
        {
            using (var memory = new MemoryStream())
            {
                using (var compression = new GZipStream(memory, CompressionLevel.Optimal))
                {
                    using (var writer = new BinaryWriter(compression))
                    {
                        Write(writer, model);
                    }
                }
                return memory.ToArray();
            }
        }

So basically I think AspIdentity fails on the connection, this error is swallowed, the Owin pipeline continues processing the request but when it hits the TicketSerializer perhaps a null value or some other bogus value is passed in which Gzip tries to zip and Boom!

Weirdo YSOD output ensues.

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

AspIdentity is not properly short circuiting OWIN request processing for this particular event.

Probably utter nonsense but I'll throw it out there anyway.




回答2:


I experienced a similar problem.

The Entity Framework __MigrationHistory Model column contains GZipped data. If the data in this column is corrupted then your app won't be able to unzip the data and you'll get the error.

In my case, the corruption occurred by trying to manually insert into this able.

My solution: delete corrupted __MigrationHistory rows and related DB changes and allow the app to migrate the database properly.



来源:https://stackoverflow.com/questions/28227020/why-am-i-getting-the-magic-number-in-gzip-header-is-not-correct-error-using-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!