【问题标题】:ConnectionStrings where do they come from? Asp.netConnectionStrings 它们从何而来? ASP.NET
【发布时间】:2016-09-12 09:43:09
【问题描述】:

我正在使用 Azure SQL 数据库在 Azure 上运行 Razor 站点,但开始收到一条连接错误消息,指出它无法连接到 App_Data localdb - 我不使用它。我还注意到我的项目在没有我要求的情况下重新创建了一个 localdb。我已经将 Asp 身份表迁移到了我的 azure sql 数据库,所以一切都在一个地方很愉快(这是个好主意吗?在我看来是这样)。反正。 localdb 消息让我感到困惑,据我所知,我的项目中没有对 localdb 的引用。我的 web.config 有 2 个连接字符串:

我已经用 Kudu 检查了这些内容,这就是网站的实际 web.config 上的内容 - 因此没有转换或发布连接字符串。但是,如果我调试天蓝色并查看 ConfigurationManager.ConnectionStrings 我看到 4 个连接字符串,而不是 2 个。最后 2 个来自我的 web.config,第 2 个是空白的,第 1 个读作:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}

我不禁想知道这是如何以及在哪里使用的?

但是观看ConfigurationManager.ConnecitonStrings[DefaultConnection] 正确的。我的数据库模型初始化如下:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    ...

所以这是相当平静的。值得一提的是,当一切都像这样发生时,我试图启用站点范围的 SSL。我已经删除了该代码,但问题仍然存在:(

这里有更多关于我看到的错误:

发生网络相关或特定于实例的错误,同时 建立与 SQL Server 的连接。找不到服务器或 无法访问。验证实例名称是否正确,并且 SQL Server 配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 定位服务器/实例时出错 指定)

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

SQLExpress 数据库文件自动创建错误:

连接字符串指定一个本地 Sql Server Express 实例 使用应用程序的 App_Data 目录中的数据库位置。 提供者试图自动创建应用程序 服务数据库,因为提供商确定该数据库 不存在。

我的 web.config:

<?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=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
      <add name="DefaultConnection" connectionString="data source=tcp:myweb.database.windows.net,1433;initial catalog=mywebtest;user id=myStdMobile;password=dfdfd;multipleactiveresultsets=True;connect timeout=30;encrypt=True;trustservercertificate=False;application name=EntityFramework" providerName="System.Data.SqlClient" />
      <add name="DeveloperConnection" connectionString="data source=tcp:myweb.database.windows.net,1433;initial catalog=mywebtest;user id=myWebDbAdmin;password=fdfd;multipleactiveresultsets=True;connect timeout=30;encrypt=True;trustservercertificate=False;application name=EntityFramework" providerName="System.Data.SqlClient" />

  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=mydbstorage;AccountKey=7Ukljdslkjasdfjkdasfkj==" />

  </appSettings>

  <system.web>
    <customErrors mode="Off" />

<roleManager 
    enabled="true" 
    cacheRolesInCookie="true" >
</roleManager>

      <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="10240" executionTimeout="3600" />
      <globalization culture="en-GB" />
  </system.web>
  <system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="10485760" />
        </requestFiltering>
    </security>

    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="PdfSharp" publicKeyToken="f94615aa0424f9eb" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.32.2608.0" newVersion="1.32.2608.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

    <location path="answers">
        <system.web>
            <authorization configSource="myadmin.config" />
        </system.web>
    </location>

    <location path="audits">
        <system.web>
            <authorization configSource="myadmin.config" />
        </system.web>
    </location>

    <location path="candidates">
        <system.web>
            <authorization configSource="myadmin.config" />
        </system.web>
    </location>

    <location path="vCandidateTables">
        <system.web>
            <authorization configSource="members.config" />
        </system.web>
    </location>

    <location path="documents">
        <system.web>
            <authorization configSource="members.config" />
        </system.web>
    </location>

    <location path="companies">
        <system.web>
            <authorization configSource="myadmin.config" />
        </system.web>
    </location>

    <location path="questions">
        <system.web>
            <authorization configSource="myadmin.config" />
        </system.web>
    </location>

    <location path="users">
        <system.web>
            <authorization configSource="myadmin.config" />
        </system.web>
    </location>

</configuration>

更新
我的错误是角色管理器 - 如果我禁用它,问题就会消失......以及角色管理,但仍然如此!这是一个步骤...

【问题讨论】:

  • 您的machine.config 中有什么内容?
  • 我认为 Azure 没有,是吗?
  • 它有,但这些被剥离了。你能在这里发布你的 web.config 吗?这些是 Visual Studio 项目向导在您创建新应用程序时设置的默认连接字符串条目。它们可以隐藏在 EntityFramework 本身的配置部分中。
  • 好的。添加。另请注意,我已将其范围缩小到启用了角色管理器。我认为如果我使用迁移来创建一个新数据库,它将在新位置创建我需要的所有内容以进行角色管理......显然不是!
  • 你检查过 'myadmin,config' 和 'members.config' 吗?

标签: c# asp.net asp.net-mvc azure azure-sql-database


【解决方案1】:

我的问题的答案是我启用了&lt;roleManager&gt;,以为我指的是 ASP.NET Identity 2。它们不一样!&lt;roleManager&gt; 启用了旧版本的身份管理,然后&lt;roleManager&gt; 元素默认为localdb。所以......这就是额外连接字符串的来源。下面是 &lt;roleManager&gt; 在 Azure 的有效 machine.config 中的样子(注意 providers -&gt; connectionStringName):

<roleManager 
   enabled="false" 
   cacheRolesInCookie="false" 
   cookieName=".ASPXROLES" 
   cookieTimeout="30" 
   cookiePath="/" 
   cookieRequireSSL="false" 
   cookieSlidingExpiration="true" 
   cookieProtection="All" 
   defaultProvider="AspNetSqlRoleProvider" 
   createPersistentCookie="false" 
   maxCachedResults="25">
   <providers>
      <clear />
      <add 
         connectionStringName="LocalSqlServer" 
         applicationName="/" 
         name="AspNetSqlRoleProvider" 
         type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <add 
         applicationName="/" 
         name="AspNetWindowsTokenRoleProvider" 
         type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   </providers>
</roleManager>

一旦我发现这个,我更新了我的 web.config 以指向我的数据库,如下所示:

  <roleManager
      enabled="true"
      cacheRolesInCookie="true"
      defaultProvider="OurSqlRoleProvider"
     >
      <providers>
          <add
             connectionStringName="DefaultConnection"
             applicationName="/"
             name="OurSqlRoleProvider"
             type="System.Web.Security.SqlRoleProvider" />
      </providers>

  </roleManager>

... 但这只是产生了一个新的错误 Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion' 这是因为 rolemanager 用于旧形式的 asp 身份,而不是 ASP.NET Identity 2。

(一个小问题,但我发现这个错误的方法只是通过源代码控制、重建,然后查看是哪个更改破坏了它。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2011-08-28
    • 1970-01-01
    • 2013-12-04
    • 2011-03-01
    • 2021-07-31
    • 2023-03-27
    相关资源
    最近更新 更多