You use Microsoft .NET Framework 4.0 to develop an ASP.NET Web application that connects to a Microsoft SQL Server 2008 database.
The application uses Integrated Windows authentication in Internet Information Services (IIS) to authenticate users.
A connection string named connString defines a connection to the database by using integrated security.
You need to ensure that a SqlCommand executes under the application pool’s identity on the database server.
Which code segment should you use?
A.
using (var conn = new SqlConnection())
{
conn.ConnectionString = connString;
SqlCommand cmd = null;
using (HostingEnvironment.Impersonate())
{
cmd = new SqlCommand(“SELECT * FROM BLOG”, conn);
}
conn.Open();
var result = cmd.ExecuteScalar();
}
B.
using (var conn = new SqlConnection(connString))
{
var cmd = new SqlCommand (“SELECT * FROM BLOG, conn);
conn.Open();
using(HostingEnvironment.Impersonate())
{
var result = cmd.ExecuteScalar();
}
}
C.
using (var conn = new SqlConneccion())
{
using (HostingEnvironroent.Impersonate())
{
conn.ConnectionString = connString;
}
var cmd = new SqlCommand(“SELECT * FROM BLOG, conn);
conn.Open() ;
var result = cmd.ExecuteScalar();
}
D.
using (var conn = new SqlConnection())
{
conn.ConnectionString = connString;
var cmd = new SqlCommand(“SELECT * FROM BLOG”, conn);
using (HostingEnvironment.Impersonate())
{
conn.Open();
}
var result = cmd.ExecuteScalar();
}
Explaination ??
Isn’t the app pool identity used by default?
The connection must be opened inside the HostingEnvironment.Impersonate() using block. This only happens in D.