I need to authenticate against a remote SQL Server using Windows Authentication. The remote SQL Server is running in another domain that does not have a trust relationship with the domain I'm currently logged into via Windows. I cannot use SQL Server authentication since the SQL Server is configured to only trust domain authentication.
Windows itself allows this type of impersonation through the user interface:
I've used WindowsImpersonationContext
class from System.Security.Principal
before, but that appears to rely upon the desired username belonging to a trusted domain.
I'm using Visual Studio 2012, and can use up to Microsoft.Net 4.5, with preferably VB, but I can easily convert code from C# if necessary.
Best Answer
For the benefit of future visitors, I'm posting some VB.Net code that allows a process to use NETONLY impersonation to authenticate against a remote server that resides in an untrusted domain:
This can be called from another piece of code like so:
The standard warning of not actually embedding passwords in code certainly applies here. In the real world my app obtains the password from an encrypted column in a database.