UserManager和组织帐户 - 无法连接到数据库(mvc 5)

时间:2015-04-03 08:25:08

标签: asp.net-mvc asp.net-identity

遇到以下问题:

我创建了具有单独用户帐户身份验证的简单mvc 5项目。 一切正常我可以添加用户,角色,一切都存储在数据库中 - 一切似乎都没问题。现在我想将认证改为组织:

  <system.webServer>
    <modules>
      <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
      <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    </modules>
  </system.webServer>
  <system.identityModel>
    <identityConfiguration>
      <audienceUris>
        <add value="myUrl" />
      </audienceUris>
      <securityTokenHandlers>      
        <remove type="System.IdentityModel.Tokens.SamlSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
        <add type="System.IdentityModel.Tokens.SamlSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <samlSecurityTokenRequirement>
            <nameClaimType value="myClaim"/>
          </samlSecurityTokenRequirement>
        </add>
      </securityTokenHandlers>
   <certificateValidation certificateValidationMode="None" />
      <issuerNameRegistry type="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry, System.IdentityModel.Tokens.ValidatingIssuerNameRegistry">
        <authority name="mySts">
          <keys>
            <add thumbprint="myThumb" />
          </keys>
          <validIssuers>
            <add name="mySts" />
          </validIssuers>
        </authority>
      </issuerNameRegistry>
    </identityConfiguration>
  </system.identityModel>
  <system.identityModel.services>
    <federationConfiguration>
      <cookieHandler requireSsl="true" />
      <wsFederation passiveRedirectEnabled="true" issuer="mySTs" realm="myUrl" requireHttps="true" />
    </federationConfiguration>
  </system.identityModel.services>

它有效 - 我可以进行身份​​验证,但是当我想将新用户(基于User.Identity.Name)添加到现有数据库时:

UserManager.CreateAsync(user);

我得到了奇怪的错误:

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: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)

虽然我没有改变连接中的任何内容,dbcontext等等。

这种行为可能是什么原因?

1 个答案:

答案 0 :(得分:0)

web.config文件中似乎遗漏了一些内容。尝试根据您的数据库和连接属性编辑以下代码,将 connectionStrings 添加到您的web.config中:

<强>的web.config:

<configuration>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0; 
Initial Catalog=aspnet-YourDatabaseName; Integrated Security=SSPI; AttachDBFilename=|DataDirectory|\aspnet-YourDatabaseName.mdf" />         

希望这会有所帮助......