I am having trouble finding documentation on how to impersonate a user and open a SqlConnection as that user.
Background:
DBAs have provided an Active Directory account that has access to a database. When using the connection string:
"Server=server\instance;Initial Catalog=mtdatabase; User ID=domain\user; Password=mypassword;";
I get an error that the user is not allowed. When using Integrated Security, it ignores the specified Username and Password, and uses the currently logged in user (me when I am debugging locally).
How can I impersonate the user with the provided information in my code to ensure that it is used to open up the connection?
Currently, I am using the following code, which is failing when trying to open the connection:
[Route("api/[controller]")]
public class HomeController : Controller
{
// GET: /<controller>/
[HttpGet]
public IEnumerable<string> Index()
{
string connString = "Server=server\\instance;Initial Catalog=mtdatabase; User ID=domain\\user; Password=mypassword;";
using (var conn = new SqlConnection(connString))
{
conn.Open();
conn.Close();
}
return new string[] { "value1", "value2" };
}
}