c# - ConnectionStrings 从哪里来? ASP.net

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

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

我已经使用 Kudu 检查了这些内容,这就是网站实际 web.config 上的内容 - 因此不存在任何转换或发布连接字符串。然而,如果我调试 azure 并查看 ConfigurationManager.ConnectionStrings 我看到 4 个连接字符串,而不是 2 个。最后 2 个来 self 的 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 时,一切都像这样发生了。我已经删除了该代码,但问题仍然存在:(

以下是有关我看到的错误的更多信息:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist.

我的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>

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

最佳答案

我的问题的答案是我有 <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>

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

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

关于c# - ConnectionStrings 从哪里来? ASP.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39447484/

相关文章:

c# - 此 C++ 代码片段在 C# 中的等效语法是什么?

c# - 为什么我的对象需要很长时间才能创建?

asp.net - Entity Framework : Insists on adding new entity in many-to-many instead of re-using existing FK

c# - MVC 4 下拉列表格式化移动应用程序

c# - 无法从类库中读取 App.config 值

c# - 验证 GUID 作为参数

c# - 如何将 System.Web.Cache 传递到我的服务层

asp.net-mvc - 使用 c# Asp .net MVC 将视频上传到 Youtube

c# - 在 ASP.NET Identity 2 中哪里可以找到生成的 PasswordReset token ?

c# - 如何在 .Net 中使用内联汇编来使用英特尔的 RDRAND