Why is IIS7 blocked from accessing my database when connecting to a website via a browser from a client machine? - asp.net

When I access the website from the same machine IIS is running on I am able to connect to the database, but not if I try to access it remotely. The only way I've managed to get it to connect is when I specify Physical Path Credentials:
click to open image
This doesn't work for me because, I am using the username as a search criteria when querying the database and this way I always get the same username (the one I specified in the physical path credentials).
I am confused because, when accessing the website locally and remotely I use the same credentials and I get different results.
This is the exception that is thrown when trying to access the site remotely:
ByNadexLoginSystem.Exception: System.Exception:
'SelectDataObject()' failed --->
System.Data.SqlClient.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: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server) --->
System.ComponentModel.Win32Exception: Access is denied --- End of
inner exception stack trace
I have tried adding folder permissions as pointed out here:
ASP.NET Application page not working on clients computers with no success whatsoever.
The SQL server is configured to allow remote connections.
I am sure it is a permissions problem I just can't find where. Any ideas?

I found the solution to my problem, I want to give credit to Sean Lange. Thanks to his question it gave me a line of investigation to follow. While going through msdns documentation here https://msdn.microsoft.com/en-us/library/bsz5788z.aspx I found this:
If SQL Server is on a different computer than the Web server, the
Windows identity must be able to flow across the network to the remote
instance of SQL Server. (Windows networks that have been configured
appropriately with Kerberos authentication are able to do this.)
However, depending on the settings in the identity configuration
element, the Windows identity established on the operating system
thread for ASP.NET applications may not be able to flow properly to
the remote SQL Server.
In IIS, only Basic Authentication logs users on with a security token
that flows across the network to a remote SQL server. By default,
other IIS security modes used in conjunction with the identity
configuration element settings will not result in a token that can
authenticate to a remote SQL Server.
Enabling Impersonation and Basic Authentication ( disabled Windows Authentication ) solved it!

Related

Error loading my web app after deploying .NET Core console app to Azure

My app is running locally without any issue but when I try to run it from Azure console I have the following error :
An error occurred while starting the application.
SqlException: Cannot open server 'Server-name' requested by the login. Client with IP address 'w.x.y.z' is not allowed to access the server. To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect.
Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
SqlException: Cannot open server 'server-name' requested by the login. Client with IP address 'ip' is not allowed to access the server. To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule
I have added IP in firewall setting on azure portal still the issue is happening
This error was caused because your IP was not added in the SQL server tab(azure portal). So you must add the IP address of your SQL Server on Azure portal
You have to update the settings of the "Firewalls and virtual networks" of your SQL Server.
Browse to your SQL Server, add the IP address and allow other azure services and resources to access the server. Refresh and wait at least 5 minutes before trying again.
I also had my ubuntu server firewall blocking my request once, so be sure your webserver is not responsible.
Have you tried enabling this setting?

ASP.net can't connect to SQL Server after being deployed to hosting site

I'm developing an ASP.net web application.
My default page is a simple login screen.
I am using Visual Studio to develop the application.
It will run using Visual Studio and when the data is entered to the text boxes and the button is clicked, it sends the request to my own server running Windows Server 2012 with SQL Server 2014 installed.
I will also mention that the ASP application is being created as a secondary application for an Android application. The Android application is using the same database over Internet connection so I know that the server and SQL server both can accept read and write calls.
I have tested the ASP application using WebClient calls to php files which connect to the database (this works on Android) and also OracleClient and SQLClient calls all of which work from Visual Studio on my workstation to the server machine. These all work fine to send and receive data from the database.
The problem is once I deploy the application to my hosting site the calls no longer seem to work. The default login page opens but after attempting to login I receive errors as follows:
Exception Details: System.ComponentModel.Win32Exception: No connection could be made because the target machine actively refused it
[Win32Exception (0x80004005): No connection could be made because the target machine actively refused it]
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
Can anyone help me with this?
It sounds like your database is accepting connections from some clients, but not your hosting server. I would try testing a SQL connection straight from the hosting Server and see if a test connection can be successful. If you can get that to work it should work from the web server.
This article Quick ways to test OLE DB Connection String on creating a empty TestConnection.udl file will be helpful. It is easier to try different connection string settings through the Data Link Properties UI. When you save the file, you can see the connection string if you open it in a text editor (assuming it is successful).
If it's not a problem with he connection string, check that you do not have a firewall blocking connections between the Windows 2012 and SQL servers. It could be the widows firewall on either machine, or another firewall at the network level.

How to setup Azure web service with sql database

I wonder how can I connect my web role to existing SQL database ? Under settings tab in properties of web role I've tried to add new setting with connection string to my database, but when i try to Login (I'm using asp.net Identity, and using code first migrations I've successfully seeded DB), I get this error:
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: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]
So what is correct way to connect my DB to web role ?
There was stupid problem with my connection string, I've solved it.
Take a look at the dashboard for your SQL Azure database. Click on the "Manage allowed IP addresses" link.
Make sure that the Windows Azure Services are set to YES under allowed services.
All The SQL Databases on Azure require a whitelist for IP ranges that can access them.

I can't display my pages on ASP.NET

I'm developing a web site with asp.net on localhost, I'm beginner. I wrote a login page and it
works well. However when I click a link, the server can't load the page and gives this error:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Any idea?
This means you either:
provided the wrong login/password for SQL while using SQL authentication
have used Windows authentication for SQL but the user that your web app is running as doesn't have a login
have provided the wrong connection details for the sql instance so it can't be found
don't have remote connections enabled for that SQL server instance
Are you using SQL server? If yes, run a debug and see what is the connection string at run time and whether you can connect to the DB using the connection string.
In case you are not using SQL server, it seems, you have extended the membership provider but not done so for the role provider. The default role provider tries to connect to SQL Server instance.

ASP.NET Membership DB connection

I can neither login from my application nor connect using the wizard to my Membership Provider on my local machine. My other DBs can connect from my application just fine. My connection string is correct, it has not been changed (I was able to login using membership previously, this is a new issue).
I created a new App to see if it could be the config, threw a login on it and registered and ran into the same issue.
This is the error message I am getting: "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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) "
Though my other DB can connect I still checked:
TCP/IP enabled
Multiple connections enabled
Firewall down
Correct instance of sql server being referred to in Machine config.
Also, my network service account has rights to the DB, but should there be an ASPNET user?
I can't think of a single thing I have changed to this machine since this was working previously.
Thank you.
EDIT DETAILS: There are no aliases, yet, but if my other connections are working that shouldn't be the issue. I understand these things can be wonky though, so that's something I'm going to try now. Thanks. I will also try and take a look at what's going on while trying to connect. Best way: event logs?
I am using Windows Auth, I'm connecting locally, using Windows 7 and SQL Server 2008 along with VS 2008. I can connect with Management Studio and query the DB, yes. NetworkService account has rights to the DB.
Check the Server name in the connection-string if the Sql-Server is in the same machine with the web application use "."(dot) without quotes instead of the computer-name in your connection string.
Have you tried sniffing it to see if there's anything you can see on the wire when the connection attempt is made?
Are there any aliases around the server/DB name? Have you tried creating one and connecting to it?
Are the other instances you can connect to on the same machine? Same SQL Server instance?
If you use impersonation, does the problem go away?
It sounds like you can connect to one database, but not the one you need. Is that correct?
The ASPNET user is a Windows XP thing. You should generally use NETWORK SERVICE for server-based deployments.
So, you can connect from Management Studio, but not from IIS? What identity are you using for the IIS AppPool?
Can you connect if you use Cassini (the web server that comes with Visual Studio)? That should pass through your user ID.

Resources