locked
How to establish Mysql Connection under Medium Trust RRS feed

  • Question

  • User17438264 posted

    My Hosting company have change its policy and now they are not supporting full trust. So I am no longer able to connect to my Mysql database using Asp.Net.

    I tried everything which was provided by dev.mysql.com and some suggestions here but nothing have worked in my case, what I tried is this

    <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      <connectionStrings>
        <remove name="LocalMySqlServer" />
        <add name="LocalMySqlServer" connectionString="server=xxxxxx; User Id=xxxxxx; Connect Timeout=60;Database=xxxxxx; password=xxxxx" providerName="MySql.Data.MySqlClient" />
      </connectionStrings>
      <mscorlib>
        <security>
          <policy>
            <PolicyLevel version="1">
              <SecurityClasses>
                <SecurityClass Name="MySqlClientPermission" Description="MySql.Data.MySqlClient.MySqlClientPermission, MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
              </SecurityClasses>
              <NamedPermissionSets>
                <PermissionSet class="NamedPermissionSet" version="1" Name="ASP.Net">
                  <IPermission class="MySqlClientPermission" version="1">
                    <add connectionString="Server=xxxx;Database=xxxx;User=xxxx;Password=xxxx;Port=3306;" restrictions="" KeyRestrictionBehavior="PreventUsage" />
                  </IPermission>
                </PermissionSet>
              </NamedPermissionSets>
            </PolicyLevel>
          </policy>
        </security>
      </mscorlib>
        <system.web>
        <compilation debug="true">
          <assemblies>
            <remove assembly="MySql.Data" />
            <remove assembly="MySql.Web" />
          <add assembly="MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
          <add assembly="MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
          </assemblies>
        </compilation>
        <authorization>
          <allow users="*" />
        </authorization>
        <authentication mode="Forms">
          <forms name="cokiename" loginUrl="login.aspx" protection="All" timeout="60" defaultUrl="Default.aspx" />
        </authentication>
        <roleManager enabled="true" defaultProvider="MySQLRoleProvider" cacheRolesInCookie="true" cookieName=".ASPXROLES" cookiePath="/" cookieTimeout="30" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="true" cookieProtection="All">
          <providers>
            <clear />
            <add name="MySQLRoleProvider" autogenerateschema="true" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="af" />
          </providers>
        </roleManager>
        <membership defaultProvider="MySQLMembershipProvider">
          <providers>
            <clear />
            <add name="MySQLMembershipProvider" autogenerateschema="true" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresUniqueEmail="false" requiresQuestionAndAnswer="false" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="0" maxInvalidPasswordAttempts="3" applicationName="af" hashAlgorithmType="SHA1" passwordFormat="Encrypted" />
          </providers>
        </membership>
        <profile>
          <providers>
            <clear />
            <add name="MySQLProfileProvider" autogenerateschema="true" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="af" />
          </providers>
        </profile>
        <customErrors mode="Off" />
        <machineKey validationKey="05C3A141953B5E16CB5334362859EF18A4C4389597059A6EBD46B35237BC6ED8C8AA28C13FC864E17B7F1DCD10D91EF915D1ED704BCDAFF24CC8DC95D1F9EEB0" decryptionKey="13BFC1542BE6CD9C54E2FFDF04F119B0FA6B19DA295DFC5B327BE6AC5787365E" validation="SHA1" decryption="AES" />
    <pages>
          <controls>
            <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
          </controls>
        </pages></system.web>
      <system.data>
        <DbProviderFactories>
          <clear />
          <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </DbProviderFactories>
      </system.data>
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="2097151000" />
          </requestFiltering>
        </security>
        <handlers>
            <add name="AjaxFileUploadHandler" verb="POST" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/>
        </handlers>
      </system.webServer>
      <location path="superadmin">
        <system.web>
          <authorization>
            <allow roles="admin,superadmin,professional,officestaff" />
            <deny users="*" />
          </authorization>
        </system.web>
      </location>
      <!--<location allowOverride="true">
        <system.web>
          <securityPolicy>
            <trustLevel name="Medium" policyFile="internal" />
          </securityPolicy>
          <trust level="Medium" originUrl="" />
        </system.web>
      </location>-->
       <!--<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <probing privatePath="bin;sitefinity\admin\bin" />
          <dependentAssembly>
            <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" />
            <bindingRedirect oldVersion="6.2.3.0" newVersion="6.4.4.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>-->
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <appSettings>
        <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
      </appSettings>
    </configuration>

    having this exception:

    [SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
       MySql.Data.MySqlClient.MySqlConnection.AssertPermissions() +0
       MySql.Data.MySqlClient.MySqlConnection.Open() +190
       MySql.Web.Common.SchemaManager.GetSchemaVersion(String connectionString) +59
       MySql.Web.Common.SchemaManager.CheckSchema(String connectionString, NameValueCollection config) +34
       MySql.Web.Security.MySQLRoleProvider.Initialize(String name, NameValueCollection config) +436
       System.Web.Configuration.ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type providerType) +412

    I have words with hosting company and I have asked them to let me know the version of mysql dlls in GAC and the quote was like this:

    There is no GAC MySql Client in our servers. Instead use the ODBC data source.

    You can access it via Plesk panel >> ODBC data sources >> add new >> connection name >> give the Server IP address >> Login ID

    Password >> test connection >> then use the connection string instead of the previous one.

    Let us know if you need any further assistance.

    It took 10 long days to reach to this conclusion, then I finally tried the mysql odbc solution, but it is not connecting to Mysql Membership using the code I have shown in web.config

    Parser Error Message: Keyword not supported.
    Parameter name: driver

    I replicate the same at my local system but with remote connectionstring for mysql odbc driver 3.51 where it stuck at Mysql Membership line in web.config.

    I hope I will surely get some solution out from here. I don't have any problem either with odbc or the way I did previously. But my major concern is to use Membership provider of Mysql connector as I have developed lot of pages on on my local system.

    Thanks in advance.

    Jaidev

     

    Friday, November 13, 2015 5:46 AM

Answers

  • User-2057865890 posted

    Hi,Jaidev

    having this exception:
    [SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
    MySql.Data.MySqlClient.MySqlConnection.AssertPermissions() +0
    MySql.Data.MySqlClient.MySqlConnection.Open() +190
    MySql.Web.Common.SchemaManager.GetSchemaVersion(String connectionString) +59
    MySql.Web.Common.SchemaManager.CheckSchema(String connectionString, NameValueCollection config) +34
    MySql.Web.Security.MySQLRoleProvider.Initialize(String name, NameValueCollection config) +436
    System.Web.Configuration.ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type providerType) +412

     

    Here is a same problem,please see
    http://stackoverflow.com/questions/18315457/net-4-0-medium-trust-level-and-mysql-connection 

    For more infomation,you could refer to
    http://dev.mysql.com/doc/refman/5.1/en/connector-net-info.html 

    I hope it's helpful to you.

    Best regards,
    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 15, 2015 10:25 PM