Login failed for user 'IIS APPPOOL\myapp' - asp.net

I had a small database app that direct connected to its database via this conn string -
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CalendarPhotos.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
This all worked fine until today I attached that database to SQL Server Management Studio so that I could quickly and easily edit some data. Up till then this database had never been attached.
Having done the edits and detached teh database, I now find that I get teh following error when browsing localhost
Cannot open user default database. Login failed.
Login failed for user 'IIS APPPOOL\Calendar Comp'.
The IIS app pool setting is currently ApplicationPoolIdentity. I do not know if it was set to that prior to the attach.
Can anyone advise how to get this thing running again please?

Related

Cannot open database. Login failed for user 'NT AUTHORITY\NETWORK SERVICE'

I'm trying to deploy my first ASP.NET app on IIS, but I'm facing an issue whenever I connect to database. Connecting to database works just fine running the app on visual studio.
I keep getting the following:
SqlException: Cannot open database "RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
My connection string looks like this:
<add name="LocalSqlServer"
connectionString="Server=(localdb)\MSSQLLocalDB;Database=RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad;Integrated Security=true"
providerName="System.Data.SqlClient" />
Like stated in the article here: https://learn.microsoft.com/en-us/archive/blogs/sqlexpress/using-localdb-with-full-iis-part-2-instance-ownership I have tried #1 "Run IIS as our Windows user" and #2 "Use LocalDB Shared Instance" of the approaches without any luck so I've moved to trying NetworkService instead of ApplicationPoolIdentity which is where I'm facing the same error (using ApplicationPoolIdentity it fails with
Login failed for user 'IIS APPPOOL.NET v4.5'") just under different user.
In my SQL Server Management Studio I've created a new user with "NT AUTHORITY\NETWORK SERVICE" name and made sure I gave "db owner" permission for the database and ticked all Server Roles. Most of the answers point out to giving permissions, but that still does not address this for me.
Any insights would be appreciated
Problem in your ConnectionString, you haven't any user content CS for Login Database.
First you must add user for database your then set your connectionString
Two Options
Create new user on your database then this user set access for RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad Create User
Enable sa user, by default disabled sa user, Enable Sa user
Finally Change connection string
if you use option 2
Server=(localdb)\MSSQLLocalDB;Database=RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad; user=sa; password=yourPassword
**For enable **
The solution in the above link is useful, Run IIS as our Windows user or Use LocalDB shared instance is an approach to the issue.
I am here just to discuss the approach provided in the documentation. In solution1, firstly we should ensure that the VS is running properly because we should ensure the Database instance also is running properly. Then change the application pool identity to the Windows account running the Visual Studio.
In solution2, since Visual Studio2017 has updated the name of the local database instance, we should change the command accordingly.
sqllocaldb share instance mssqllocaldb IIS_DB
Subsequently, apply it to the connection string.
Data Source=(localdb)\.\IIS_DB;Initial Catalog=mydatabase;Integrated Security=True
Here is a detailed description of this issue.
https://codemegeek.com/2018/05/13/configure-iis-to-us-localdb/
Feel free to let me know if there is anything I can help with.

Classic ASP connection works; .Net fails. Why?

This works in a Classic ASP Web site:
sConnString = "Provider=SQLOLEDB;Data Source=MySerer;Initial Catalog=MyDB;Integrated Security=SSPI;"
In a .Net web site, the following connection string gets a "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'" error.
<add name="Production"
connectionString="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
Both run in the same Application Pool on IIS, and both sites are set to use the same Admin user to connect, as set under Basic Settings.
Seems that, though the .Net site runs as an Admin, it fails to pass that along to the SQL Server. Not only that, if I hardcode the login/password into the connection string, it returns the same error about anonymous login.
What's the deal?
Turns out I had more than one problem. The login issue was resolved when I moved the app to its own Application Pool and set the Identity of the pool to an actual admin account. (I'm open to using a non-admin account; just needed to identify/solve the problem first.)
The other issue was some missing views and stored procs in the DB the DotNet site was supposed to query. Yes, Classic ASP could connect to that and another database, but connectivity was all I tested with Classic ASP, not actually performing the desired query. The DotNet site could connect to and query the DB that had the views and stored procs, but gave a 404 error when trying to query the DB that lacked those objects.
I can't say I'm totally clear on things, because the DotNet site sometimes could
get data from the DB with the objects even before moving it to its own Application Pool, and sometimes not. At least I'm up and running. I will continue to experiment.

Asp.net LocalDB (SQL) Login Failed for user 'SQLLogin'

Overall Goal is to create a webapp that has a local DB that multiple people from outside the network can access at one time.
I am trying to use a SQL Login for the connection string for a local DB in my asp.net website. When trying to access the site locally from development I get.
Cannot open database "|DataDirectory|\tatcrm.mdf" requested by the login. The login failed.
Login failed for user 'TatUser'.
I am using the following connection string in web.config:
<add name="Tattoo" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=|DataDirectory|\tatcrm.mdf;User ID=TatUser;Password=SecretPassword" providerName="System.Data.SqlClient"/>
Inside the LocalDB I have used the following to create a Login & User.
While using the tatcrm DB:
CREATE LOGIN TatUser WITH PASSWORD = 'SecretPassword';
CREATE USER TatUser FOR LOGIN TatUser;
EXEC sp_addrolemember 'db_owner', 'TatUser'
The login and User are created but when I try to connect It says invalid login. What is going on here? What am I missing?
Authentication Mode is mixed as well.
Mix Mode Authentication Set True Image
Screenshot of localDB in asp.net project and the users/login
I have tried creating the DB in SSMS first then move over to App_Data to check if the user worked. See images Below.
New SQL Db created in SSMS to test TatUser works
Now TatUser does not work in VS when migrated DB over to App_Data Folder
I was unable to make this work. I just ended up using a DB from the localhost's SQLServer instead of having a Local DB. Works as intended, but it would have been nice to have the DB attached to the web application.
Thank you all for your help in trying to get this to work for me.

Cannot open database "MyDatabaseName" requested by the login. The login failed. Login failed for user 'IIS APPPOOL\MyAppName

I have created a web application. I am using SQL 2012 express as a database. Its working fine when I am running on debug mode. When I deployed that application on local IIS it's throwing an error
Cannot open database "databaseName" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\MyApplicationName'.
My connection string is <add name="DefaultConnection" connectionString="Data Source=HAPPY\SQLEXPRESS;uid=sa;password=123;Initial Catalog=dwm;integrated security=sspi;" providerName="System.Data.SqlClient" />
I am able to login with this login detail on my SQL 2012 express. I am attaching a screenshot .
You are using SSPI Integrated Security, thus it is trying to use Windows authentication. Try setting to False:
connectionString="Data Source=HAPPY\SQLEXPRESS;uid=sa;password=123;Initial Catalog=dwm;integrated security=False;" providerName="System.Data.SqlClient"
Since your web application uses a SSPI Integrated Security.
You have those options :
Either add the IIS APPPOOL\MyApplicationName as a login and give the required rights to the database dwm.
But since this login is probably not known from the SQL Server you might have to change the Login used by your WebAppPool with a service account from the AD that is shared with your SQL Server.
Or change the connection string with a SQL Login (which has the required rights) - it is not recommended to use the sa login also. Also using this method reduces the security.
connectionString="Data Source=HAPPY\SQLEXPRESS;uid=mySqlLoginForMyApplicatioNName;password=itsPassword;Initial Catalog=dwm;integrated security=False;" providerName="System.Data.SqlClient"
For me EF was not creating the database and navigating to the application gave me the following -
"Cannot open database “MyDatabaseName” requested by the login. The
login failed. Login failed for user 'IIS APPPOOL\MyAppName"
Restarting the website within IIS solved my issue.

Server Error in '/' Application, Cannot open database "" requested by the login. The login failed

I have created a website using ASP.net with simple data connection which is working fine in localhost. when I want to publish the same, I am getting this error. "Cannot open database "Abcdb" requested by the login. The login failed. Login failed for user 'XYZ-Domain\XYZ-SVR-01$'.
I don't have the user 'XYZ-Domain\XYZ-SVR-01$' in my SQL Server (SQL Express 2012).
I have my server in a virtual machine .
my connection string is "Data Source=XYZ-05-VM1\SQLEXPRESS;Integrated Security=True;Initial Catalog=Abcdb".
do I have to create a user in my database as shown above or else can I use existing database user. I am very new to this technology. Kindly help me.
Try it
<connectionStrings>
<add connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Abcdb;Integrated Security = SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
The error means that the server could be reached and the database name is correct. The problem is that the login is invalid.
From the login name I suppose that you have set Integrated Security=SSPI in your connection string and therefore try to authenticate with the account that your application is running under at the SQL Server. On your development machine, this is your user account that on a typical developer machine will be granted high access rights to the local SQL Server.
After you have deployed your application to IIS, it will run in an AppPool that is basically a process under a specific user account (the AppPool account). You need to grant this user account access to the database. In your case, this account is the Network Service account of the server you have your application deployed to. So you need to either grant the Network Service account access to the database or follow this - cleaner - approach:
Create a new Service account in your domain that is dedicated to your application.
Configure your AppPool to run under this account. Assert that the account has the minimum amount of access rights on the server that is possible.
Grant this account access to your database.
If you want to check the database connection settings, have a look at the connectionStrings section in your web.config. This link shows the available settings for SQL Server.

Resources