Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open database "test" requested by the login. The login failed. Login failed for user 'xyz\ASPNET'

People also ask

Can not open database requested by the login the login failed SQL Server?

In the security panel, click the option under Server authentication for SQL Server and Windows Authentication mode. Click OK to save the settings and close the Properties dialog box. Add a new SQL login by expanding the Security folder and clicking the Logins folder. Right-click the Logins folder and click New Login.

Can not open database?

If you are receiving the error message “Cannot open database. It may not be a database that your application recognizes, or the file may be corrupt.”, check if the database has reached or exceeded the 2 GB limit. If so, running MS Access built-in 'compact and repair' utility may help.

Can not open database mssql?

Fix/Workaround/Solution:In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.


Well, the error is pretty clear, no? You are trying to connect to your SQL Server with user "xyz/ASPNET" - that's the account your ASP.NET app is running under.

This account is not allowed to connect to SQL Server - either create a login on SQL Server for that account, or then specify another valid SQL Server account in your connection string.

Can you show us your connection string (by updating your original question)?

UPDATE: Ok, you're using integrated Windows authentication --> you need to create a SQL Server login for "xyz\ASPNET" on your SQL Server - or change your connection string to something like:

connectionString="Server=.\SQLExpress;Database=IFItest;User ID=xyz;pwd=top$secret"

If you have a user "xyz" with a password of "top$secret" in your database.


  • Either: "xyz\ASPNET" is not a login (in sys.server_principals)
  • Or: "xyz\ASPNET" is set up but not mapped to a user in the database test (sys.database_principals)

I'd go for the 2nd option: the error message implies the default database is either not there or no rights in it, rather than not set up as a login.

To test if it's set up as a login

SELECT SUSER_ID('xyz\ASPNET') -- (**not** SUSER_SID)

If NULL

CREATE LOGIN [xyz\ASPNET] FROM WINDOWS

If not NULL

USE test
GO
SELECT USER_ID('xyz\ASPNET')

If NULL

USE test
GO
CREATE USER [xyz\ASPNET] FROM LOGIN [xyz\ASPNET]

I had this problem and what solved it for me was to:

  • Go to the Application pools in the IIS
  • Right click on my project application pool
  • In Process Model section open Identity
  • Choose Custom account option
  • Enter your pc user name and password.

For me the database was not created and EF code first should have created it but always endet in this error. The same connection string was working in aspnet core default web project. The solution was to add

_dbContext.Database.EnsureCreated()

before the first database contact (before DB seeding).


The best solution for the login problem is to create a login user in sqlServer. Here are the steps to create a SQL Server login that uses Windows Authentication (SQL Server Management Studio):

  1. In SQL Server Management Studio, open Object Explorer and expand the folder of the server instance in which to create the new login.
  2. Right-click the Security folder, point to New, and then click Login.
  3. On the General page, enter the name of a Windows user in the Login name box.
  4. Select Windows Authentication.
  5. Click OK.

For example, if the user name is xyz\ASPNET, then enter this name into Login name Box.

Also you need to change the User mapping to allow access to the Database which you want to access.


Most times, it's not a login issue, but an issue with creating the database itself. So if there is an error creating your database, it would not be created in the first place. In which case if you tried to log in, regardless of the user, login would fail. This usually happens due to logical misinterpretation of the db context.

Visit the site in a browser and REALLY read those error logs, this can help you spot the problem with you code (usually conflicting logic problems with the model).

In my case, the code compiled fine, same login problem, while I was still downloading management studio, I went through the error log, fixed my db context constraints and site started running fine....meanwhile management studio is still downloading


The Issue

The error presents itself as a message similar to this:

Cannot open database "DATABASE NAME" requested by the login. The login failed. Login failed for user XYZ.

  • The error cannot usually be rectified by a simple Visual Studio or full-computer restart.
  • The error can also be found as a seemingly locked database file.

The Fix

The solution is laid in the following steps. You will not lose any data in your database and you should not delete your database file!

Pre-requisite: You must have installed SQL Server Management Studio (Full or Express)

  1. Open SQL Server Management Studio
  2. In the "Connect to Server" window (File->Connect object explorer) enter the following:
    • Server type : Database Engine
    • Server name : (localdb)\v11.0
    • Authentication : [Whatever you used when you created your local db. Probably Windows Authentication).
  3. Click "Connect"
  4. Expand the "Databases" folder in the Object Explorer (View->Object Explorer, F8)
  5. Find your database. It should be named as the full path to your database (.mdf) file
    • You should see it says "(Pending Recovery)" at the end of the database name or when you try to expand the database it won't be able to and may or may not give you an error message.
    • This the issue! Your database has crashed essentially..
  6. Right click on the database then select "Tasks -> Detach...".
  7. In the detach window, select your database in the list and check the column that says "Drop Connections"
  8. Click OK.
  9. You should see the database disappear from the list of databases. Your problem should now be fixed. Go and run your application that uses your localdb.
  10. After running your application, your database will re-appear in the list of databases - this is correct. It should not say "Pending recovery" any more since it should be working properly.

The source of the solution: https://www.codeproject.com/Tips/775607/How-to-fix-LocalDB-Requested-Login-failed