Using IIS 6 App pool identity to connect to SQL server - iis-6

I'm using IIS 6 in Server 2003 and SQL server 2008 which sits ona seperate box. Now i would like to connect using the Application pool identity of the website to SQL server.
But somehow whenever i display the identity in page_load method it always displays the windows account using which site was browsed. I have given the details below
Server : Windows Server 2003
Database : SQL server 2008 . Web server and DB sit on different machines.
WebSite : Uses a custom app pool that runs using a custom domain account. Website has anonymous disabled and windows authentication enabled. Impersonation is not enabled in web.config.
I'm displaying the credentials using the below code :
Response.Write(HttpContext.Current.User.Identity.Name.ToString());
WindowsIdentity id = WindowsIdentity.GetCurrent();
Response.Write(id.Name);
Now i need a way of using the identity of app pool to connect to database. Please let me know how to do this. If i set the website anonymous account as the app pool identity everything works fine but i dont want to do that. Now authentication just flows as anonymous to the SQL server and it throws exception.

I'm not sure about IIS6, but in IIS8... In the advanced settings for the application pool in questions, there is an identity section. Set this to the windows credentials you want to use. Also set 'Load application profile' to true.
Then in your application, you just need to setup your connections string to use a trusted_connection instead of an SQL server username and password.
Just remember to set the windows account to have a password which never expires, unless you'll remember to update the app-pool when it does change (or the pool will stop.)
Dave

This sounds like you have Windows Authentication enabled and are using IE to connect to the site? In this case the Windows Identity is passed through to IIS.

Related

How to make IIS use AppPool Identity to connect to SQL Server instead of NT Authority\IUSR?

I've set up a Windows Server 2019 box with IIS, PHP 8 and SQL Drivers, and SQL Server 2019 Express. I created a test PHP application and corresponding application pool, then set up a test database. When trying to connect using sqlsrv_connect, I get the following error:
Login failed for user 'NT AUTHORITY\IUSR'.
(There is more there in the SQL error but it's just the raw error array output and Stack Overflow doesn't like it.)
Now, I believe I have everything configured correctly to use the application pool identity instead of NT AUTHORITY\IUSR. I copied the setup from another older (Server 2012) web server. Screenshots of my setup:
Anonymous Authentication using App pool identity in IIS
App pool identity added on database in SQL Server
Am I missing a setting somewhere, or does Server 2019 handle application pool identities differently than server 2012? I've tried following instructions like on this excellent SO page:
IIS AppPool to SQL Server permissions (adding NT AUTHORITY\IUSR)
But it's like the server doesn't care that I've told it to use the application pool.
UPDATE EDIT: MYSTERY SOLVED
Apologies all, this was a case of extreme user error. I put the "test" directory under wwwroot, ala c:\inetpub\wwwroot\test. I was accessing it as www.domain.com/test, when what I needed to do was set it to run as test.domain.com. It was running under the wwwroot app pool (which in this case was using IUSR) because that is how I was accessing it. Thank you everyone who helped me troubleshoot this!
Apologies all; this was a case of extreme user error.
I put the test directory under wwwroot, i.e., c:\inetpub\wwwroot\test. I was accessing it as www.domain.com/test, when what I needed to do was set it to run as test.domain.com. It was running under the wwwroot application pool (which, in this case, was using IUSR) because that is how I was accessing it.
Thank you everyone who helped me troubleshoot this!

can you map a windows login to an sql server authentication user?

this is for the purpose of cutting an asp.net web application over from using an sql server authentication connection string to using a windows authentication connection string.
See How to: Access SQL Server Using Windows Integrated Security.
If your application runs on a Windows-based intranet, you might be able to use Windows integrated authentication for database access. Integrated security uses the current Windows identity established on the operating system thread to access the SQL Server database. You can then map the Windows identity to a SQL Server database and permissions.
To connect to SQL Server using Windows integrated authentication, you must identify the Windows identity under which your ASP.NET application is running. You must also be sure that the identity has been granted access to the SQL Server database. This topic includes a code example that displays the current Windows identity of the ASP.NET application.
As well as the application of this toward an intranet, you can use it for a public website by setting the Application Pool to run as a custom user that has a Windows logon to the SQL Server.

SQL Unique User ID and workstation

I have the following scenario:
A web application deployed over an NLB and a SQL Cluster.
In the database each table has a trigger to track activities done by users.currently the triggers are saving HostName (which is not useful in case of such web deployment).
Connection to the db is impersonated with the account "SA", Is there a possible way to obtain the
connected user or the workstation sending the request inside SQL or I need to pass User Info into the db from withing the applications Code.
The DBMS is SQL and the web application is based on .NET framework
You need to pass in the user id from the code unless you set up Kerberos pass through to use Windows Authentication. That is out of scope for this answer.
More importantly, why are you running a web app using "sa" on the SQL cluster. This is idiocy. Use a "least privilege" windows service account to run your IIS App Pool and use this to connect to SQL Server.

Exception when deploying to IIS: Login failed for user 'IIS APPPOOL\DefaultAppPool'

I'm working through some WCF examples in "Windows Communication Foundation 4 Step By Step". My resulting application runs fine as long as the service is hosted in casini. It fails when I deploy the service to local IIS. When deployed to IIS, I can browse to the svc page in IE. That works.
According to the book, page 41, the app pool account needs to be a member of the db_owner role in my database. The author suggests, after verifying the correct address of the service (already did that), checking the permissions of the app pool account.
How do I verify which account is used by the application pool hosting my WCF service? Currently using the default app pool and IIS APPPOOL\DefaultAppPool is in the db_owner role. IIS APPPOOL\ASP.NET v4.0 is also in the db_owner role.
The exception is...
System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\DefaultAppPool'.
How do I troubleshoot this issue? Thanks!
Solution found here:
Open IIS
double click your PC name under Connections
Click Application Pools
Select your app pool (DefaultAppPool)
Then under actions on the right click Advanced Settings,
Go to Process Model section and
click on Identity.
Now select NetworkService.
I had the same problem. Issue was that I had "IntegratedSecurity=True;" in my connection string but I was using sql authentication and passing in credentials at the same time. I removed the IntegratedSecurity piece and everything worked.
First thing you need to clear if you are using windows authentication and you are not mentioning any username password in your connection string then:
What happens when you run your code through localhost:
when you run your wcf test client from localhost, it will be able to communicate to database as local debug mode application is calling database by your account's service. So it has access to database because devenv.exe is running under your user account.
But when you deploy your web service in IIS. Now understand this service runs under IIS not under your account. So you need to assign access rights to IIS service to access the sql server for windows authentication.
Here your web service would not be able to communicate to the SQL server because of access rights issue.
So if you are using windows authentication to connect your database, you just have to change the IIS Application pool settings.
You need to change IIS Application pool's identity to local System.
Below are the Steps for windows authentication WCF:
•Open IIS (windows+R (run) then type inetmgr, then click ok)
•double click your PC name under Connections
•Click Application Pools
•Select your app pool (DefaultAppPool)
•Then under actions on the right click Advanced Settings:
•Go to Process Model section and
•click on Identity.
•Now select LocalSystem.
Now open your sql server management studio:
open run-> then type ssms then press ok
in ssms, login using your windows authentication account.
open security tab
expand logins tab
then you will be able to view your account.
Now open properties of your account
go to userMapping
then select the database you want to connect
then check the role membership services you want to use for the selected database
click ok.
add Trusted_Connection=True; property in your connection string.
Save it & deploy the web service.
Restart app pool.
you will be able to connect the database now.
if not exists
(select * from sys.server_principals where name='IIS APPPOOL\DefaultAppPool')
create login [IIS APPPOOL\DefaultAppPool] from windows;

"Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'." to SQL Server 2005

I'm trying to migrate a legacy application we have to Windows Server 2008 x64 and IIS7. It's written in Classic ASP and connects to a SQL Server 2005 database.
However, when the page runs, I receive the error:
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
The connection string I'm using is: Driver=SQL Server; Server=SERVERNAME; Initial Catalog=DBNAME;
I can't see any reason for it to be using the anonymous logon as when it was running on my 32-bit Win2k3 server, it accessed the SQL Server using DOMAINNAME\SERVERNAME$.
I have the following settings.
SQL Server 2005 - running in mixed mode.
IIS7 Application Pool - Allow 32-bit applications set to True.
I've also added the server as a user on the SQL Server.
I've tried a few things now and I'm starting to run out of ideas.
There are some solutions.
use SQL authentication, instead of SSPI. It will work since your database is in mixed mode.
If for any reasons you won't change the authentication.
The NT AUTHORITY\ANONYMOUS LOGON is strange, by the way, just as if the database server and the app servers are not on the same domain. Please check this until proeding further.
1) Create an user in Active Directory
2) Create a application pool on IIS7 and as "Identity" , the user you've created
3) Grant this user the rights and roles you want on the DB
I think I've fixed it. Moving to Win2k8 means that I needed to change the connection string driver to SQL Native Client;.
Set the Anonymous Authentication credentials to be your Application Pool Identity.
(default is Specific User --> IIS User). Then you can use a Trusted Connection in your connection string as it will now use the AppPoolIdentity account.
Well, that failed login attempt is a classic example of code running under the "Local Service" account, rather than (what you had on Win2K3), where it was running under the "Network Service" account. Now you just need to find where that setting is.
For either the OP or future readers, you can:
1) Either use a dedicated service account, as suggested by Johan above, or
2) Use the AppPoolIdentity account and go to "Authentication", then select "Windows Authentication" and enable "ASP .NET Impersonation".
If you still get the same result after #2, you likely have the "double hop" issue cited by Damien - this is where the web server can authenticate you, but it does not allow you to pass those credentials on to the database for security reasons without using Kerberos. You would then need to set the server as "Trusted for Delegation" in AD and set up Kerberos as the authentication provider in IIS. This site helps with that: http://technet.microsoft.com/en-us/library/ee675779.aspx