ConnectionStrings它们来自哪里?Asp.net

本文关键字:Asp net ConnectionStrings | 更新日期: 2023-09-27 18:06:34

我正在Azure上运行一个带有Azure SQL数据库的Razor站点,但开始收到一条连接错误消息,说它无法连接到App_Data localdb,我不使用它。我还注意到我的项目善意地重新创建了一个localdb,而我在某个时候没有要求它。我已经将Asp标识表迁移到了我的azure sql数据库中,所以一切都很好地放在一个地方(这是个好主意吗?我觉得是这样(。无论如何localdb消息让我很困惑,因为据我所知,我的项目中没有引用localdb。我的web.config有2个连接字符串:

我已经向Kudu检查了这些,这就是网站实际的web.config中的内容,所以没有转换或发布连接字符串。是的,如果我调试azure并查看ConfigurationManager.ConnectionStrings我看到4个连接字符串,而不是2个。最后两个来自我的web.config,第二个为空,第一个读作:

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-定位服务器/实例时出错指定(

描述:在执行期间发生未处理的异常当前web请求。请查看堆栈跟踪以了解更多信息有关错误的信息以及错误在代码中的来源。

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="answerswers">
        <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>

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

ConnectionStrings它们来自哪里?Asp.net

我的问题的答案是,我启用了<roleManager>,认为我指的是ASP。NET Identity 2它们不一样<roleManager>启用了旧版本的身份管理,然后<roleManager>元素默认为localdb。所以这就是额外连接字符串的来源。以下是<roleManager>在Azure的有效machine.config中的外观(注意providers -> 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>

但这只是产生了一个新的错误找不到存储过程';dbo.aspnet_CheckSchemaVersion';这是因为角色管理器用于旧形式的asp身份,而不是asp。NET Identity 2。

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