Is there a simple out of the box way to impersonate a user in .NET?
So far I\'ve been using this class from code project for all my impersonation requirements.
I'm aware that I'm quite late for the party, but I consider that the library from Phillip Allan-Harding, it's the best one for this case and similar ones.
You only need a small piece of code like this one:
private const string LOGIN = "mamy";
private const string DOMAIN = "mongo";
private const string PASSWORD = "HelloMongo2017";
private void DBConnection()
{
using (Impersonator user = new Impersonator(LOGIN, DOMAIN, PASSWORD, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50))
{
}
}
And add his class:
.NET (C#) Impersonation with Network Credentials
My example can be used if you require the impersonated logon to have network credentials, but it has more options.