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

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.

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.

Login failed for user '' IIS

Im currently trying to deploy my .NET MVC Application to IIS 7.5.
There is a database connection problem that im trying to figure out. In localhost, everything seems fine, however, when I deploy my app to remote server, the app runs, without connecting to database. For example I have a VisualLeadController that has getMonthlyLeadsByYear() method that connects to database and retyrns data. When I try to execute it from brower by typing
http://staging2.landornet.com/WebLeadsVisualizer/VisualLead/getMonthlyLeadsByYear
It generates this error:
Exception Details: System.Data.SqlClient.SqlException: Login failed for user ''.
The steps that I followed:
1-Removed integrated security=True from WebConfig connection strings
2- Changed applicationpool identity from applicationPoolIdentity to local system.
Still now working... anyone has any idea?
When deploying your web app to your remote server, you will need to use a SQL connection string that contains an explicit username and password associated with a SQL account. For example,
Data Source=SERVER;Initial Catalog=DATABASENAME;User ID=SQLUSERNAME;Password=SQLUSERPASSWORD
To use this connection string, you will need to first use SQL Management Studio to create a SQL login with a username and password, and give that user access to your database. Then log out out of SQL Management Studio and log back in using that user/password to make sure that it correctly can access your database. Hopefully then your web app should be able to connect to the database.

Can't gain access to local SQL for modification

I've been given a backup of a production SQL database to begin writing an application against.
I've got it running as a local instance and using Windows Authentication and I can READ the information, however I cannot figure out how to get IIS to use my Windows Authentication to log into the SQL server when running a local ASP.Net website.
I don't have the authority on the DB to add users, and I'm getting an error of System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'. when I try to access a page that reads the database.
I don't think I will be able to communicate with the DB admin for a while to get him to fix this up for me. Is there anything I can do here?
Your asp code is using the same identity that IIS runs under - the Network Service account. To get it to use your credentials, you can either hard-code them into a connection string or you can use impersonation (see this link: http://msdn.microsoft.com/en-us/library/aa292118%28VS.71%29.aspx)
The gist of the link:
Put this in your web.config file: <identity impersonate="true" />
-t.
You need to create an account in SQL Server for the Network Service account,ex via SSMS via the "Security" section, right click on "Logins" and select add. You will be adding a Windows Account, you can then lookup and validate the name "NETWORK SERVICE". Then switch to the "User Mapping" section and grant permission to view your database.
reference : http://msdn.microsoft.com/en-us/library/ms998320.aspx#paght000015%5Fsqlserver
2nd Alternative
If "integrated security=sspi" is there in your connection string than you should:
Turn on integrated auth in IIS
Turn on windows auth in asp.net
Turn on imerposation in asp.net
Hopefully it will help.

Can't connect to database on server

I cannot connect to my SQL Server database when running app on server.
Everything runs fine when debugging but when I run on the server as ASPNET the user is unable to login.
I created the dataabse in a test project then simply connected to this db. My connection string is
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Temp\Barry\fsr\FSR.mdf;
Integrated Security=True;Connect Timeout=30;User Instance=True
So this asp app runs on IIS 5 and when deployed the initial select works fine as my gridview that I have a binding to shows data but when I run the program (update the db) I hit the following:
[SqlException (0x80131904): Cannot open user default database.
Login failed.
Login failed for user 'hostxxxxx\ASPNET'.]
I am not creating this database programmatically as mentioned previously, simply connecting to an existing database.
The database is located on my c: - but I have added user ASPNET. How can I add this db to the program as a resource rather than reference a copy on c:?
My first question is this: If you have control of the server, why are you using an attached database. From:
AttachDbFilename=C:\Temp\Barry\fsr\FSR.mdf
There is no reason to attach if you can control the server. Attach the database to the actual instance in SQL Server, not with the bastardized version you have above. The bastardized version is useful on an ISP that does not give you access to SQL tools. Beyond that, it is more work than it is worth.
The second problem you have is authentication. There are a couple of ways to handle this. The easiest is set up a SQL user to access the database. If every user will have login credentials, you can keep the Windows Authentication method, but you have to turn off anonymous access, so every user GETS authenticated. As long as anon is a choice in IIS, it will default to anon and you will have issues. The third way is to impersonate a user for database access. I guess the fourth is open your database wide open, but I don't suggest destruction of security to make something "work".
If you have your database on a server, you need to use a server-based connection string - something like:
Data Source=servername\SQLEXPRESS;database=FSR;
Integrated Security=True;Connect Timeout=30;
Your user needs to have a login on the server, and a user in the appropriate database, in order to connect successfully.
See the ConnectionStrings.com web site for a huge list of possible connection strings - and what their settings mean and how you can change those.
You need to get into your database and assign the proper privileges to the account that is trying to access the database, which in this case looks like the built-in ASPNET account. Instead of the ASPNET account, you should use the NETWORK SERVICE account. You can change this through IIS.

Unable to open database remotely

Pls help me, My newly created web application is using a SQLServer Express database. If I run the application in local machine its working fine, but if I try to open the database connection from another system in the network it is giving error Cannot open database . I have used connection string --
conn.ConnectionString = "integrated security=SSPI;data source=USER-CCE8C22332\SQLEXPRESS;" +"persist security info=False;initial catalog=Marketee";
i think you need to give access to the ASPNET user to access your database if you are using integrated security. The reason is that when the user connects to the website, the server-side processing is done via the user set up in IIS in teh application pool.
To test, what you can do is switch from integrated security to passing a username and login and see if you can connect then.

Resources