App ignoring initial catalog attribute from connection string - asp.net

I do face a rather strange behavior at customer site:
My ASP.NET application tries to connect to a database using a connection string, specified in the web.config.
This works fine in all cases but one - this specific customer needed to switch integrated security on, due to security permissions.
Strangely, now the application tries to connect to another database and not the one defined as 'initial catalog'.
I've already double-checked the connection string for any typos.
There are no USE statements in the queries. Basically there's nothing else telling the application where to connect to but this connection string.
If relevant - it is using the EntityClient provider.
The connection string (some parts changed of course):
<add name="ConnName" connectionString="metadata=... provider connection string="data source=SERVER;initial catalog=DB;persist security info=True;Integrated Security=true;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

Problem solved - there is a View involved which hasn't been created correctly and was pointing to that other database.

Related

How to connect ASP.NET app to remote SQL Server

I need help connecting my Web App to a remote database (SQL Server).
I have tried many suggested solutions but I can't seem to come right.
This is how I connect to a local database, it works 100%:
<add name="DBCS" connectionString="Data Source=serverName;Initial Catalog=MVNE_Website;Integrated Security=True" providerName="System.Data.SqlClient" />
My ASP.NET Web App is hosted on one server, and the database is on a separate server.
The remote DB server is 100% configured to allow remote connections and firewall rules also adhere to the connection protocols. I think it is just my connection string that is incorrect but I don't know why??
Here it is(conn string for remote SQL server)
<add name="DBCS" connectionString="server=serverIP\serverName; database=MVNE_Website; Integrated Security=True" providerName="System.Data.SqlClient" />
I don't use a username or password when connecting to this remote SQL Server so I did not see a point in adding it in the conn string?
There can be a few reasons why this will not work. Here are 2 common ones:
Your web application will pass the username the application pool is running under, (which by default is some system user) to SQL Server. Change this to be a service account which has access to SQL Server.
If you are hopping across 2 or more servers to pass the credentials between IIS and SQL Server, you may need to implement Kerberos, which is a way to preserve the credentials. This is a complex network configuration thing.
Check point 1 first.
:/
In my web.config file custom errors mode was on RemoteOnly, so I turned it off and saw that my connection string was never the problem, the actual problem was that the app was trying to insert null into a primary key field that does not allow null, i never set the PK to auto increment
.. sorry and thanks

Azure App Service Connection string settings not available custom Session provider

I added all my connection strings to the App Service -> Settings screen.
I'm using Redis to share Sessions between the different instances. Everything is good if I have the connection in the ConnectionString section in the web.config but if I remove it from there and just add it to the Connection string section under the settings screen then it all breaks.
I'm running a classic .net web application on .net 4.7
I've tried the following so far:
Added a connection string in web.config without a value hoping it will overwrite it
Completely removed the connection strings in the web.config file.
Just for a test, I added an extra connection string which I can later retrieve and show on a web page.
How early do the connection strings from azure get applied? Is there a workaround or is this a known issue (haven't managed to find anything about it)?
EDIT: Here is a sample of what I'm trying to do.
I have a connection string called <add name="redis.sessions" value=""/> and a session state provider that points to that connection string
<add name="redis" type="Sitecore.SessionProvider.Redis.RedisSessionStateProvider, Sitecore.SessionProvider.Redis" applicationName="private" connectionString="redis.sessions" pollingInterval="2"/> and in the session provider on Initialization it tries to initialise the Redis connection with a blank connection string. Even through one has been provided in the App Service Settings.

Unable to connect using sql authentication through an asp.net web app

I cannot get my web app to connect to the database when running the app. I can connect in the SQL Server object explorer and I took the connection string from the connections properties
Here is my connection string :
<add name="Quotes.DAL.QuotesConnection"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=TestDB;Integrated Security=False;User ID=sa;Password=***;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"
providerName="System.Data.SqlClient" />
I can connect as SA and I can create the database that it points to in Management Studio. When my app tries to connect via Entity Framework I get the following error :
System.Data.SqlClient.SqlException (0x80131904): Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,
I'm running Windows 7 as a home dev machine so there is no Active Directory.
Can anybody explain why this might happen?
I have found that this is probably because I have Entity Framework in another assembly. I created a new web application and its connection string works. When it try the same connection string in my DAL it fails with this same error!
Thanks
It may be because of setting Integrated Security=False, I have never set it to false. I normally use just the bare minimum Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword; when setting up my connection strings with sql authentication, only adding things as I need them.
The other issue I see is using SA to connect to the database. This breaks almost every best practice in the book and I would recommend creating an app specific password for when you roll out the program. The reason being is the GOD ACCESS account for the server will have its password in plain text in the app.config file. Just something to keep in mind.
EDIT: After some comment discussion the connection string was in the wrong config file.

SQL connection string ignoring specified username & password?

I’m having a problem with my SQL Connection strings not using the authentication provided in the connection string. I have written an ASP.NET application using self-tracking entities and I need to access data from two different servers. The first server, SQLSERVER1 is using windows authentication and is on the domain and im able to pull my data just fine. The second server connection however, is on another box off the domain and I need to use a specific login and password to access the data. I confirmed that the credentials im using in the connection string can connect to the server just fine.
On my web-server setup I have my app pool in IIS7 running under its own credential “Domain\AppPoolUser”.
For some reason, whenever I try to call data from SQLSERVER2 after publishing to the webserver im getting the following error, even though I have specified the credentials in my connection string and they are valid. The connection works fine however, if im debugging the application locally : Login failed for user 'DOMAIN\AppPoolUser'.
It seems like it’s not using the credentials I have specified in the connection string, and instead trying to connect as the application pools credentials. How can I setup the connection string to use the credentials I have provided?
Here is my connection strings:
<add name="ADATrackingContext" providerName="System.Data.SqlClient" connectionString="Server=SQLSERVER1;Database=ADATracking;Integrated Security=True;" />
<add name="PatientContext" providerName="System.Data.SqlClient" connectionString="Server=SQLSERVER2;Database=Patients;uid=PatientUser;password=123456;Integrated Security=False;" />
Not sure if it matters, but the user should be User Id= and not uid=
The ADATrackingContext connection string will attempt to login using the Domain\AppPoolUser domain account since you have set the Integrated Security = true. Since you don't have Domain\AppPoolUser as a login for your db, you will get the error you describe.
Double check to make sure you are passing the correct connection string to your DbContext.
It looks like the error was being caused beacuse I was trying to pull the data before my client page was authenticated to the ADATracking connection string. I moved the data call over to the HomeConstroller.cs instead of the Global.asax file and all is working now. My mistake.

SQL Express connection string hell ASP.Net

SQL Express 2005 is running locally. I have a project written by another person running on the same machine. All I want to do is connect to it, can't be that hard right?
This is the one I use in my old classic ASP code to hit another database running on the same instance:
Provider=SQLOLEDB;Data Source=MYLAPTOP\MSSMLBIZ;Persist Security Info=True;User ID=TestUser;Password=letmein;Initial Catalog=TestDB
But trying a version of that makes the .net code throw a wobbler as he's written it using SQLServer drivers so its not liking the Provider stuff.
Here is the orginal connection string from his code:
Server=(local);Initial Catalog=TheDatabase;User Id=TheUser;Password=ThePassword;
I've been to http://www.connectionstrings.com/sql-server-2005 and tried several of the options from there, these all get "SQL Server does not exist or access denied" (what a lovely mixed error message that is!):
Data Source=localhost;Integrated Security=True;Initial Catalog=TheDatabase
Data Source=localhost\SQLEXPRESS;Integrated Security=True;Initial Catalog=TheDatabase
Data Source=MyLaptop\SQLEXPRESS;Integrated Security=True;Initial Catalog=TheDatabase
Server=MyLaptop\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUser;Password=ThePassword;
I've created logins for MyLaptop/IUSR_MyLaptop, MyLaptop/ASPNET, MyLaptop/IWAM_MyLaptop in SQL Express and given them all read/write permissions to my DB and set their default DB to be TheDatabase.
What the heck am I doing wrong and how can I debug the problem some more?
UPDATE: Special Thanks to Chris for all his pointers, got there in the end, if you are having similar problem please read all the comments there are lots of links and tips on how to track them down.
Can you advise exactly what is in the config?
Are you using the block - in which case a valid connection string would be:
<add name="connection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost\MSSMLBIZ;Initial Catalog=TheDatabase;Integrated Security=True" />
or
<add name="connection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost\MSSMLBIZ;Initial Catalog=TheDatabase;Integrated Security=False;User Id=TheUser;Password=ThePassword;Application Name=AppName;" />
Or are you getting the connection string from app settings - in which case I guess your provider is set in code inside the app itself?
With that error message in your comment you should run through the items in http://blogs.msdn.com/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx
I presume the instance is running and does allow connections over tcpip?
Shouldn't your datasource read: Data Source=localhost\sqlexpress too?
You don't mention granting rights for 'TheUser' to access the database on the server - if you're restored from another server you may had a sid mismatch.
Try running
sp_update_users_login 'report'
against the db in question.
If it returns the user account in the report try:
sp_update_users_login 'update_one', 'theuser', 'theuser'
to remap things.

Resources