1.6. Connecting to SQL Server Using Integrated Security from ASP.NET

Problem

You want to coordinate Windows security accounts between an ASP.NET application and SQL Server.

Solution

Connect to SQL Server from ASP.NET using Windows Authentication in SQL Server:

  1. Begin by creating a new ASP.NET Web Application project.

  2. Add the following elements to the Web.config file within the <system.web> element:

    	<authentication mode="Windows" />
    	<identity impersonate="true" />
  3. Add a connection string to AdventureWorks on the local machine to the configuration file Web.config by updating the <connectionStrings> element within the <configuration> element as follows:

    	<connectionStrings>
    	    <add name="AdventureWorks" providerName="System.Data.SqlClient"
    	        connectionString="Data Source=(local);
    	        Integrated security=SSPI;Initial Catalog=AdventureWorks;"/>
    	</connectionStrings>

The C# code in Default.aspx.cs in the project IntegratedSecurityFromAspNet is shown in Example 1-7.

Example 1-7. File: Default.aspx.cs for IntegratedSecurityFromAspNet solution

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace IntegratedSecurityFromAspNet
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sqlText = "SELECT TOP 10 * FROM Person.Contact";
            string connectString = 
                ConfigurationManager.ConnectionStrings[
                "AdventureWorks"].ConnectionString;
            DataTable dt = new DataTable(  );
            SqlDataAdapter da = new SqlDataAdapter(sqlText, connectString);
            da.Fill(dt);

            foreach (DataRow row in dt.Rows)
                Response.Write(row["ContactID"] + " - " + row["LastName"] +
                    ", " + row["FirstName"] + "<br/>");
        }
    }
}

The output is shown in Figure 1-6.

Discussion

Connecting to a SQL Server database provides two different authentication modes:

Windows Authentication

Uses the current security identity from the Windows user account to provide authentication information. It does not expose the user ID and password and is the recommended method for authenticating a connection.

SQL Server Authentication

Uses a SQL Server login account providing a user ID and password.

Output for IntegratedSecurityFromAspNet solution

Figure 1-6. Output for IntegratedSecurityFromAspNet solution

Integrated security requires that all application users are on the same domain so that their credentials are available to IIS. The following areas of the application need to be configured:

  • Configure the ASP.NET application so that Integrated Windows Authentication is enabled and Anonymous Access is disabled.

  • The web.config file establishes the authentication mode that the application uses and that the application will run as or impersonate the user. Add the following elements to the web.config file within the <system.web> element:

    	<authentication mode="Windows" />
    	<identity impersonate="true" />
  • The connection string must contain attributes that tell the SQL Server that integrated security is used. Use the Integrated Security=SSPI attribute-and-value pair instead of the User ID and Password attributes in the connection string. The older attribute-and-value pair Trusted_Connection=Yes is also supported.

  • Add users and groups from the domain and set their access permissions as required.

By default, ASP.NET applications run in the context of a local user ASPNET on IIS. The account has limited permissions and is local to the IIS computer and therefore not recognized as a user on remote computers. To use SQL Server on a different computer than IIS, run the web application in the context of a domain user recognized on both IIS and SQL Server computers.

In addition to the areas identified where IIS and SQL Server are on the same computer, the following additional items must be configured if the SQL Server is on a different domain:

  • Ensure that the mapped domain user has required privileges to run the web application.

  • Configure the web application to impersonate the domain user. Add the following elements to the web.config file for the web application:

    	<authentication mode="Windows" />
    	<identity impersonate="true" userName="domain\username"
    	        password="myPassword" />

Get ADO.NET 3.5 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.