在 godaddy 上使用 MySQL 和实体框架的安全异常

发布于 2024-08-05 10:47:09 字数 4447 浏览 3 评论 0原文

在 godaddy 上使用实体框架时,我们遇到了 SecurityException 。该实体已针对 MySQL 存储进行配置。 (v. 6.1.2) 不过,这个异常有点奇怪...查看异常堆栈,似乎意味着如果我们在站点的任何位置打开与 MySQL 的连接,那么我们应该得到相同的异常;然而,直接打开MySQL连接似乎在网站的另一部分工作......

这是验证:

using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
  connection.Open();
  ...
}

有人遇到类似的问题吗?

完整的错误堆栈跟踪如下:

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags) +0
   System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +470
   System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +1051
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +111
   System.Resources.ResourceManager.CreateResourceSet(Stream store, Assembly assembly) +357
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +471
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +583
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +583
   System.Resources.ResourceManager.GetString(String name, CultureInfo culture) +74
   MySql.Data.MySqlClient.Resources.get_PerfMonCategoryName() +40
   MySql.Data.MySqlClient.PerformanceMonitor..ctor(MySqlConnection connection) +43
   MySql.Data.MySqlClient.MySqlConnection.Open() +434
   System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +173
   System.Data.EntityClient.EntityConnection.Open() +96
   System.Data.Objects.ObjectContext.EnsureConnection() +81
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +46
   System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +44
   jet.Controllers.WorkOrder.WorkOrderView..ctor() +219
   jet.Controllers.WorkOrder.WorkOrderView.get_Instance() +29
   jet.Controllers.WorkItemController.Index() +11
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +53
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +382
   System.Web.Mvc.Controller.ExecuteCore() +123
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +144
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

We are getting a SecurityException when using Entity framework on godaddy. The entity has been configured against a MySQL store. (v. 6.1.2) A bit of weirdness with the exception though... Looking at the exception stack it seems to imply that if we open up a connection to MySQL anywhere in the site, then we should get the same exception; however, opening up a MySQL connection directly seems to be working in another part of the site...

Here's the verification:

using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
  connection.Open();
  ...
}

Anyone run across a similar issue?

The complete error stack trace is as follows:

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags) +0
   System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +470
   System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +1051
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +111
   System.Resources.ResourceManager.CreateResourceSet(Stream store, Assembly assembly) +357
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +471
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +583
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +583
   System.Resources.ResourceManager.GetString(String name, CultureInfo culture) +74
   MySql.Data.MySqlClient.Resources.get_PerfMonCategoryName() +40
   MySql.Data.MySqlClient.PerformanceMonitor..ctor(MySqlConnection connection) +43
   MySql.Data.MySqlClient.MySqlConnection.Open() +434
   System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +173
   System.Data.EntityClient.EntityConnection.Open() +96
   System.Data.Objects.ObjectContext.EnsureConnection() +81
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +46
   System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +44
   jet.Controllers.WorkOrder.WorkOrderView..ctor() +219
   jet.Controllers.WorkOrder.WorkOrderView.get_Instance() +29
   jet.Controllers.WorkItemController.Index() +11
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +53
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +382
   System.Web.Mvc.Controller.ExecuteCore() +123
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +144
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆躺在深渊里 2024-08-12 10:47:09

奇怪的一个。 GoDaddy 共享托管 ASP.NET 应用程序在中等信任下执行,并且性能计数器创建可能需要完全信任,但性能计数器创建并不是此处失败的原因。 (如果失败,它不会传播出去,因为 perf 计数器创建异常被 mySQL 客户端代码吞没)。

相反,在创建性能计数器之前尝试访问字符串资源失败。失败在于 MySQL 客户端的 Resources.Designer.cs 中的这行代码:

return ResourceManager.GetString("PerfMonCategoryName", resourceCulture)

需要尝试一些事情,按难度递增的顺序:

  1. 确保您的应用程序的 BIN 目录中有 MySQL 客户端 DLL,并且您不尝试从 GoDaddy 的 GAC 加载客户端 DLL。如果可以避免的话,切勿依赖 GoDaddy 提供的二进制文件!

  2. 切换到 EN-US 文化,这样客户端就不必去寻找另一个资源 DLL,并查看问题是否消失。

    切换到 EN-US 文化,这样客户端就不必去寻找另一个资源 DLL,并查看问题是否消失。

  3. 从源代码构建 .NET 客户端,而不是将 DLL 从二进制发行版复制到应用程序的 BIN 目录中。这将使此类问题更容易调试,因为 mySQL 代码不会是黑匣子。而且,在紧要关头,您可以更改有问题的资源获取调用(或对区域设置进行硬编码)! :-)

顺便说一句,如果您想在连接字符串中设置“Use Performance Monitor=false”来尝试逃避问题,请不要打扰。无论设置如何,有问题的代码都会执行:

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

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);
            }
        }
    }
醉城メ夜风 2024-08-12 10:47:09

贾斯汀是当场。问题是 godaddy 在他们的 gac 中有一个旧版本的 MySql dll,而实体框架正在拾取它!

这是修复方法。 (将此应用到 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>

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>
冷情 2024-08-12 10:47:09

我只是将 trust level="Full" 添加到我的 Web 配置文件中,并且它起作用了

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文