MS SQL Server dotnet core connection issues - sql-server

I'm using ASPNetBoilerplate and attempting to deploy docker images of an ASP.NET MVC/Vue.js app. The app builds and runs fine on development environments with a trusted connection to a local SQL Server. When I try to deploy to docker running on Ubuntu 20 attempting to connect to SQL Server running on a Windows 2019 Server I am consistently getting
Login failed for user '[removed]'.
Reason: Password did not mach for the login provided. [Client xxx.yyy.zzz.aaa]
I can use those exact same credentials to connect to the database server using either Azure Data Studio or SQL Server Management Studio.

It turns out that dotnet core is a bit picky about how a SQL Server is called in the connection string. I had to include the port, even though it is the standard port.
Working Connection String:
Server=172.16.0.19,1433; Database=Db; User Id=Uid; Password=Pass;
Non-working Connecting String, throwing a Login error as listed in the question:
Server=172.16.0.19; Database=Db; User Id=Uid; Password=Pass;

To Use Docker with Asp.net Boiler Plate I try this connection string and it works fro me
{
"ConnectionStrings": {
"Default": "Server=10.0.75.1,1433; Database=MyProjectDb; User ID=sa;Password=PASSWORD';Trusted_Connection=False;MultipleActiveResultSets=True;"
},
Note : 10.0.75.1 is the default docker internal switch

Both Local & Staging is working
"ConnectionStrings": {
"Default": "Server=host.docker.internal,1433; Database=ms19Db;User=sa; Password=yourStrong(!)Password;"
},

Related

Unable to connect .NET Core MVC Web API with SQL Server on Linux

I am using .NET Core MVC API (.NET Core 6.0), actually just C# in general.
I need to deploy a .NET API on a linux server. However, it seems to that I can't connect to the SQL Server that is running on the same server.
This is the error from .NET
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000001, 11): Resource temporarily unavailable
at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, ValueStopwatch stopwatch)
This is the error I get from curl
{"error":true,"message":"Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started."}
Keep in mind that both the API and the database are on the same server.
I think the problem comes from .NET. I am able to connect to the database on the server using the API locally.
This is the connection string in my appsettings.json
"ConnectionStrings": {
"ConnStr": "Data Source=<PUBLICIP>;Initial Catalog=<DBNAME>;User Id=<USER>;Password=<PASS>;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",=
"lockerConStr": ""
}
Any help would be greatly appreciated!
Things I have tried:
Changed Data Source to (local) - (doesn't work)
Added Encrypt= false;Trusted_connection=True; - (doesn't work)
Connect to the SQL Server from local version of the .NET API (successful)
Connect to the SQL Server using a python script running from the same server (successful)
Specified Driver in the connection string (doesn't work)
The python script (not sure if this would help)
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=<PUBLICIP>;'
'Database=<DBNAME>;'
'UID=<USER>;'
'PWD=<PWD>;'
'Trusted_Connection=no;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
I deleted the whole project on the server and cloned the git back.
After carefully reading the error, I noticed that it's running using my local connection string (not sure why, I changed the connection string on the server). I also rebuilt the solution and republished it a few times. Not sure what changed.
But I finally got it to work!

Problem connecting asp.net core app to SQL server in a docker container

I have setup asp.net core applicaiton using SQL Server Management Studio on Windows OS, to be able to run same project on Mac OS I have setup docker container and running SQL server using azure data studio for management.
However I have tried various ways, it's not connecting and I think problem is with connection string..
Connection String
{
"ConnectionStrings": {
//"DefaultConnection": "Data Source=localhost; Initial Catalog=BalDict; Integrated Security=false; MultipleActiveResultSets=True;",
"DefaultConnection": "Server=localhost;Database=BalDict;user=sa;password=MySecurePass;MultipleActiveResultSets=true"
},
I am getting following error
SqlException: Cannot open database "BalDict" requested by the login. The login failed.
Login failed for user 'sa'.

Issue logging into SQL Server database on network from Docker container on Windows

I am testing out Docker on Windows. I created a simple .net core console app that accesses a SQL Server on the network. We access SQL Server with Integrated Security=True, using our Windows credentials. Everything works fine outside of Docker.
I created an image and started a container.
During DB connect it throws this exception:
SqlException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
The issue is not with network accessibility, but with login onto SQL Server. I can ping SQL Server host from a container.
Connection string:
Data Source=<server name>;Initial Catalog=<db name>;Integrated Security=true;Connection Timeout=300
Any tips on how to mitigate this? I tried changing the connection string to user/pwd, but that didn't work.
Wondering if there is a way to start the container as a different windows account or somehow telling the container to start the process under a certain user?
Try connection: "Data Source=localhost;Initial Catalog=<db name>;User Id=SA;Password=<password>"

Dotnet core application cannot connect to SQL server using IIS, only when using IIS Express or Kestrel

I have a .NET Core web application that fetches some data from a SQL Server. I connect to the SQL Server over the internet.
When I run the application with IIS Express or Kestrel, I can connect to the database server and retrieve data. I can also successfully connect to the database server using SQL Server Management Studio.
Here is the connection string I'm using:
"ConnectionStrings": {
"DefaultConnection": "Server=my-sql-server.someurl.com; Initial Catalog=my-sql-database; User ID=my-user-id; Password=my-password;"
}
But when I try to run the same website, on the same machine, in IIS, I get the following error message:
SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No such host is known.)
I'm suspecting this has something to do with the dotnet core/IIS combination, and not a firewall/network/authentication issue, but I might be wrong.
The issue was simply that I was looking at the wrong connection string.
I have appsettings.json containing one connection string and appsettings.Production.json containing another.
When running the application with IIS Express or Kestrel, the first one is used, but when I publish the app and then run it under IIS, the environment is set to Production, and the production settings are used instead.
The problem was that the production connection string pointed to a database server that didn't exist anymore.
I solved this issue by adding LocalSystem to Identity in Web site Application pool.
Steps:
Select website application Pool
Right click and click Advanced Setting
Click on Identity, and change it to LocalSystem
You need to enable TCP/IP protocol on sql server. Please use following link:
Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?

How to Expose SQL Server Hosted on Intranet to Connect Remotely

I am trying to remote connect to a SQL Server that is hosted on a company intranet. Ultimately, I would like to connect in with something like tedious and get the data into a web application via a REST API.
I followed this tutorial: https://blogs.msdn.microsoft.com/friis/2016/08/25/setup-iis-with-url-rewrite-as-a-reverse-proxy-for-real-world-apps/
Entered the client_net_address:1433 for the inbound rule.
Disabled SSL Offloading
Entered the public facing IP of the server in the outbound To: field but changed the last digits. e.g. xxx.xxx.xxx.100
When I try logging in with tedious I get a connection timeout error using the following config:
let config = {
userName: "**",
password: "**",
server: "xxx.xxx.xxx.100",
options:{
port: 1433,
database: "db_name",
encrypt: true
}
}
What steps must I take to connect remotely to a SQL database hosted on an intranet?
Currently I VPN in and can query from SQL Server 2014 Management Studio.
This feature is working for node module mssql version 3.3.0 and SQL Server 2008.
Use npm install mssql#3.3.0 and connectivity should work properly.
Version 4 of the mssql module has breaking changes.

Resources