无法打开登录请求的数据库“aspnetdb”。登录失败

Dmi*_*iyd 5 c# asp.net login

我可以使用我的登录名登录和浏览。但是一旦我按下页面上的超链接,它就会抛出这个:

Cannot open database "aspnetdb" requested by the login. The login failed.
Run Code Online (Sandbox Code Playgroud)

用户“DIMA-00AA1DA557\Dima”登录失败。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:无法打开登录请求的数据库“aspnetdb”。登录失败。用户“DIMA-00AA1DA557\Dima”登录失败。

为什么要这样做/...

我认为我的连接字符串和会员资格提供商一切正常:

<connectionStrings>
  <clear />
  <add name="LocalSqlServer" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Integrated Security=SSPI; Initial Catalog=aspnetdb" /> 
  <add name="YourGuruDB" connectionString="Data Source=DIMA-00AA1DA557;Initial Catalog=model;Integrated Security=True"/>
  <add name="modelConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='D:\Documents and Settings\Dima\My Documents\Visual Studio 2010\WebSites\WebSite10\App_Data\ASPNETDB.MDF';Integrated Security=True;User Instance=True;Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
  <authentication mode="Forms">
    <forms name=".MyCookie"
           loginUrl="~/Registration.aspx"
           protection="All"
           timeout="30" path="/" >
      <credentials passwordFormat="MD5">
        <user name="user" password="ca064d0730abfeb09e383a0e82e65f73"/>
      </credentials>
    </forms>
  </authentication>

  <roleManager enabled="true"/>

  <membership defaultProvider="MyMembershipProvider">
    <providers>
      <clear/>
      <add name="MyMembershipProvider"
           type="System.Web.Security.SqlMembershipProvider"
           connectionStringName="modelConnectionString"
           minRequiredPasswordLength="1"
           minRequiredNonalphanumericCharacters="0"
           enablePasswordReset="true"
           maxInvalidPasswordAttempts="1000"
           passwordAttemptWindow="4000"
           enablePasswordRetrieval="true"
           requiresUniqueEmail="false"
           passwordFormat="Encrypted"
           applicationName="/WebSite10" />
    </providers>
  </membership>
Run Code Online (Sandbox Code Playgroud)

Zha*_*uid 3

正如“Rune FS”在评论中指出的那样,您已将数据库连接字符串配置为使用集成身份验证,这是一件好事,因为您没有在配置文件中存储显式的用户名/密码。

但是,您需要知道它从哪里获取“集成”凭据 - 在本例中,运行 IIS(或 VS Dev Server)的用户的凭据用于连接到数据库 - 如果它使用您的 Windows 登录名,我假设您使用的是 IIS Express 或 VS 开发服务器?

我相信您遇到的问题是,虽然您启用了角色管理器,但尚未配置它,并且您启用了默认LocalSqlServer连接字符串,该连接字符串还指向名为的数据库aspnetdb- 默认情况下,这将尝试使用默认角色管理器提供程序,它使用 LocalSqlServer 连接。

修改<rolemanager />元素为:

<rolemanager enabled="true" defaultProvider="MyRoleProvider" >
  <providers>
    <clear />
    <add name="MyRoleProvider" 
         applicationName="/" 
         connectionStringName="modelConnectionString"
         type="System.Web.Security.SqlRoleProvider" />
  </providers>
</rolemanager>
Run Code Online (Sandbox Code Playgroud)

LocalSqlServer如果您不使用连接字符串,我还建议删除它以避免这种混乱。