【问题标题】:Security Exception using MySQL and Entity Framework on godaddy在 Godaddy 上使用 MySQL 和实体框架的安全异常
【发布时间】:2010-12-01 19:48:55
【问题描述】:

在 godaddy 上使用 Entity 框架时,我们会收到 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

【问题讨论】:

  • 我投票结束这个问题,因为我们不是 GoDaddy 的客户支持。

标签: mysql entity shared-hosting securityexception


【解决方案1】:

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

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

return ResourceManager.GetString("PerfMonCategoryName", resourceCulture)

按照难度递增的顺序尝试一些事情:

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

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

  3. 从源代码构建 .NET 客户端,而不是将 DLL 从二进制分发版复制到应用的 BIN 目录中。这将使此类问题更易于调试,因为 mySQL 代码不会是黑盒。而且,在紧要关头,将让您更改有问题的资源获取调用(或硬编码语言环境)! :-)

顺便说一句,如果您想在连接字符串中设置“使用性能监视器=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);
            }
        }
    }

【讨论】:

    【解决方案2】:

    贾斯汀在现场。问题是 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>
    

    【讨论】:

    • 很高兴为您提供帮助。顺便说一句,如果对您有帮助,请随时支持我的回答! :-)
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多