Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open database "aspnetdb" requested by the login. The login failed

I am able to login and browse with my login name. But as soon as I press a hyperlink on my page it throws this:

Cannot open database "aspnetdb" requested by the login. The login failed.

Login failed for user 'DIMA-00AA1DA557\Dima'.

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.

Exception Details: System.Data.SqlClient.SqlException: Cannot open database "aspnetdb" requested by the login. The login failed. Login failed for user 'DIMA-00AA1DA557\Dima'.

Why does it do it/...

I think everything is okay with my connection string and membership provider:

<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>
like image 857
Dmitry Makovetskiyd Avatar asked Oct 11 '22 22:10

Dmitry Makovetskiyd


2 Answers

As "Rune FS" points out in the comments, you've configured your Database Connection Strings to use Integrated Authentication, which is a good thing, because you're not storing explicit usernames/passwords in your config files.

However, you need to be aware of where it's getting the "integrated" credentials from - in this instance, the credentials of the user that IIS (or the VS Dev Server) is running under are used to connect to the database - if it's using your Windows login name, I assume you're using either IIS Express or the VS Devevelopment Server?

I believe the problem you've got is that while you've enabled the role manager, you haven't configured it, and you've left the default LocalSqlServer connection string enabled also pointing to a database called aspnetdb - by default this will attempt to use the default role manager provider, which uses the LocalSqlServer connection.

Modify the <rolemanager /> element to read:

<rolemanager enabled="true" defaultProvider="MyRoleProvider" >
  <providers>
    <clear />
    <add name="MyRoleProvider" 
         applicationName="/" 
         connectionStringName="modelConnectionString"
         type="System.Web.Security.SqlRoleProvider" />
  </providers>
</rolemanager>

I'd also recommend removing the LocalSqlServer connection string if you're not using it to avoid this sort of confusion.

like image 117
Zhaph - Ben Duguid Avatar answered Oct 14 '22 05:10

Zhaph - Ben Duguid


That Windows user isn't allowed to connect to that database and use it.

Check to make sure:

that you have the proper server name in your connection string. Is it really localhost? Or did you perhaps install SQL Server Express and it ended up being (local)\SQLExpress instead??

that the server you're connecting to has a login for Philip-Desktop\Philip (check in Object Explorer -> (your server) -> Security -> Logins)

that the database ASPNETDB has a user based on that login so that you can use that database (check in Object Explorer -> (your server) -> Databases -> (your database) -> Security -> Users)

that your admin.aspx page is truly referencing that connection string you've given (WIJLConnectionString1). Is there by any chance a separate web.config in your Admin folder that e.g. has a different connection string, and the admin.aspx page references that connection string instead??

like image 44
sajjad abdi Avatar answered Oct 14 '22 03:10

sajjad abdi