Security Exception using MySQL and Entity Framework on godaddy

泄露秘密 提交于 2019-11-28 13:12:39

Weird one. GoDaddy shared-hosting ASP.NET apps execute under Medium Trust, and perf counter creation probably requires full trust, but perf counter creation is not what's failing here. (and if it failed, it wouldn't propagate out since perf coutner creation exceptions are swallowed by mySQL client code).

Instead, it's failing trying to access a string resource, before perf counter creation. The failure is with this line of code in Resources.Designer.cs of the MySQL client:

return ResourceManager.GetString("PerfMonCategoryName", resourceCulture)

A few things to try, in increasing order of difficulty:

  1. ensure you have the MySQL client DLLs in your app's BIN directory and you're not trying to load the client DLL out of GoDaddy's GAC. Never depend on GoDaddy-supplied binaries if you can avoid it!

  2. switch to EN-US culture so the client doesn't have to go hunting for another resource DLL, and see if the problem goes away.

  3. Build the .NET client from source code, instead of copying DLLs from a binary distribution into your app's BIN directory. This will make problems like this easier to debug since the mySQL code won't be a black box. And, in a pinch, will let you change the problematic resource-fetching calls (or hard-code the locale)! :-)

BTW, in case you're tempted to set "Use Performance Monitor=false" in your connection string to try to evade the problem, don't bother. The problematic code gets executed regardless of that setting:

    public PerformanceMonitor(MySqlConnection connection)
    {
        this.connection = connection;

        //// this line is where it bombs
        string categoryName = Resources.PerfMonCategoryName;

        //// this line is affected by connection string setting
        if (connection.Settings.UsePerformanceMonitor && procedureHardQueries == null)
        {
            try
            {
                procedureHardQueries = new PerformanceCounter(categoryName,
                                                              "HardProcedureQueries", false);
                procedureSoftQueries = new PerformanceCounter(categoryName,
                                                              "SoftProcedureQueries", false);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }
    }

Justin was spot on. Problem was godaddy has an older version of the MySql dll in their gac and the entity framework was picking it up!

Here's the fix. (Apply this to web.config)

<configuration>
  ...
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d"/>
        <bindingRedirect oldVersion="5.0.7.0" newVersion="6.1.2.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  ...
</configuration>

I simply added trust level="Full" to my Web config file, and it worked

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