How to Use trusted connection when SQL Server and web server are on two separate machines.

Source: Internet
Author: User
(Ref: http://weblogs.asp.net/achang/archive/2004/04/15/113866.aspx)

Recently, in our consideration to make the production server environment more secure, one of the things we looked into was securing the database connection information between the Web server and SQL Server. I need Ed much help from attending devdays 2004 and also from doing some research on the Internet on this matter. there are some options when it comes to securing the connection string between a database server and a web server. i'll just list a few here:

1 use a trusted connection with integrated authentication.

2 use the aspnet_setreg.exe utility to encrypt the connection string and store it in the registry. You can refer to these two articles for detailed instructions on how to do this: http://support.microsoft.com/default.aspx? SCID = KB; en-US; 329290 and http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnnetsec/html/secnetht11.asp

3 using dpapi (Data Protection Application Programming Interface) to secure the connection string. You can refer to these articles: http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnnetsec/html/secnetht08.asp and http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnsecure/html/windataprotection-dpapi.asp and http://www.codeproject.com/system/protected_data.asp

In this blog, I'll focus more on Option 1-using a trusted connection. you see, it's very straightforward to configure a trusted connection when the SQL Server and web server are on the same machine. for Windows 2000 Server or xp pro machines, you just need to make sure the local "ASPnet" account (under which ASP. NET application runs) has appropriate access to the database that your application will be using. for Windows Server 2003, you need to make sure that the "Network Services" account (or "iis_wpg" group-of which the "Network Services" is a member) has the appropriate access to the database. you can follow a step-by-step procedure to configure this here: http://www.asp.net/faq/AspNetAndIIS6.aspx#4 (under the section titled "supporting integrated authentication with SQL Server ").

the problem arises, however, when the database server and the Web server are on two separate machines (assuming they are on the same network ). the issue lies in the fact that the local accounts/Credentials ("ASPnet" in Windows 2000 Server and Windows XP Pro ., "Network Services" in Windows Server 2003) under which ASP. net worker process runs are local accounts (local to the Web server ). therefore the database server on a separate machine will not be able to see/recognize these accounts. so if you try using the same steps mentioned above to configure a trusted connection between the Web server and the SQL Server, you will most likely see an error message such as "Login Failed for user '(null )'. reason: not associated with a trusted SQL server connection. ", when you try to launch your asp. NET application.

One solution is to set up a domain account with minimal access (the same level of access as the local "ASPnet" or "Network Services" account on the Web server) and grant this account the appropriate access to the database on the SQL server that your web application will be accessing. then, you can use impersonation to make your asp. NET application run under the domain account that you just creat Ed. (does anyone know of a way to set up the trusted connection without impersonation? I wowould very much like to know how we can avoid using impersonation and any overhead associated with it. some postings mentioned making the local accounts "ASPnet" or "Network Services" on both servers to have the same password, but I don't think this is a good solution ). here are some tips/steps on using impersonation for trusted connection with a domain account:

1 when creating the domain account, make sure it has the proper level of access to run ASP. NET applications. you can refer to this page for a list of directories and the permissions that this domain account must have to them. for security purposes, don't give this domain account any more permission than you have.

2 assign the domain account the necessary permission to the database (you can still follow the instructions at http://www.asp.net/faq/AspNetAndIIS6.aspx#4. Just make sure you choose the domain account that you just created ).

3 In order to turn on impersonation, there are two options:

. in the web. config File of your application, add the following line: (put it somewhere between the tags) However this really defeats the purpose of using the trusted connection to avoid having to include the credential and the password in the connection string. if you use this method, you will still need to encrypt the credential and the password in order to keep this information safe.

B. (This is the better method in my opinion) in the web. config File, add the following: <identity impersonate = "true"/> (this time without the user name and password ). then, from IIS manager, right click on the virtual directory assigned to your application and select "properties ". click on the "Directory Security" tab, then under "Anonymous Access and authentication control" click "edit ". if you are going to allow anonymous access to the application (not requiring users to login with Windows Authentication each time they access this web application), make sure "Anonymous Access" checkbox is checked. then uncheck "allow IIS to control password ". in the "user name" field, type in (or you can browse to) the domain account (domain \ User) and type in the password for the domain account in the "password" field (by default IIS uses IUSR_machinename for the anonymous account, you can refer to this page if you wocould like to find its default password in case you want to change it back later for some reason ). then under "authenticated access", make sure none of the boxes are checked. press "OK" and "OK" to save the settings and exit IIS.

4 then in Web. config you can use either one of these connection strings:

A. "Data Source = yourservername; initial catalog = databasename; Integrated Security = sspi"

B. "Server = yourservername; database = databasename; trusted_connection = true"

Hopefully, by using the tips and steps above, you shocould be able to get the trusted connection to work between the SQL server and the web server.

I hope this long-winded blog will be helpful to some. I don't claim to be an expert in this, so as I mentioned earlier, if anyone has a better solution for processing the trusted connection, I am all ears. I really wowould like to know if there is anyway to use trusted connection without impersonation.

Here are also some good references/discussions related to this topic:

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vbcon/html/vbtskaccessingsqlserverusingmappedwindowsdomainuser. asp

Http://weblogs.asp.net/cschittko/archive/2004/03/05/85035.aspx

Http://support.microsoft.com/default.aspx? Kbid = 316989

Http://dotnetjunkies.com/Newsgroups/microsoft.public.dotnet.framework.aspnet.security/2004/3/22/75451.aspx

Http://dotnetjunkies.com/Newsgroups/microsoft.public.dotnet.framework.aspnet.security/2004/3/22/74067.aspx

Http://www.winnetmag.com/Article/ArticleID/9002/9002.html

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnnetsec/html/secnetap05.asp

Http://www.kamath.com/tutorials/tut002_iisanon.asp

Http://www.microsoft.com/technet/community/columns/insider/iisi0102.mspx#XSLTsection124121120120

Update: Frans Bouma mentioned that running Web server on a domain may not be according to Microsoft's best practice for security. cocould someone confirm this? Is there a better way then to configure a trusted connection When IIS and SQL Server are on separate machines without the use of impersonation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.