How do you do Impersonation in .NET?

.net, c#, impersonation

Solution

Here is some good overview of .NET impersonation concepts.

- Michiel van Otegem: WindowsImpersonationContext made easy

- WindowsIdentity.Impersonate Method (check out the code samples)

Basically you will be leveraging these classes that are out of the box in the .NET framework:

- WindowsImpersonationContext

- WindowsIdentity

The code can often get lengthy though and that is why you see many examples like the one you reference that try to simplify the process.

Problem

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. Is there a better way to do it by using .NET Framework? I have a user credential set, (username, password, domain name) which represents the identity I need to impersonate.

Original source

Related problems