Impersonation Class
Provides access to the impersonation functions.
Inheritance Hierarchy
SystemObject
  MailBee.SecurityImpersonation

Namespace: MailBee.Security
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public class Impersonation : IDisposable

The Impersonation type exposes the following members.

Constructors
  NameDescription
Public methodImpersonation
Creates an instance of Impersonation class.
Public methodImpersonation(String)
Creates and unlocks an instance of Impersonation class.
Top
Methods
  NameDescription
Public methodDispose
Releases the resources associated with the Impersonation object.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLogoff
Terminates the impersonation mode initiated with LogonAs(String, String, String) call and reverts back to the user account the current thread was created on.
Public methodLogonAs
Makes the current thread impersonate the specified user account.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Properties
  NameDescription
Public propertyIsImpersonated
Gets whether the impersonation mode is on and the current thread is being impersonating another user.
Public propertyLastResult
Gets a numeric code of the last error.
Public propertyThrowExceptions
Gets or sets whether the object will throw exceptions on errors.
Top
Remarks

This class can be used to let the current thread programmatically impersonate another user. This can be useful if the application needs to access resources which belong to another user.

If you need to impersonate web application based on which user accessed it, refer to ASP.NET impersonation section of MSDN library.

To get the name of the user account being used by the current thread, call System.Security.Principal.WindowsIdentity.GetCurrent().Name.

Note Note
To use this class, make sure MailBee.NET Security is licensed (MailBee.NET Objects full bundle does include the license).
Note Note
This class is not available in .NET Standard 2.0 edition and in UWP edition (as it relies on Win32 API).
Examples
This sample displays the current user name, then impersontates the current thread and displays the current user name again (it's now different), then reverts back to the original user account and displays its name.
using System;
using System.Security.Principal;

using MailBee;
using MailBee.Security;

class Sample
{
    static void Main(string[] args)
    {
        // Display the current domain\user name (such as "OurCompany\JohnDoe").
        Console.WriteLine(WindowsIdentity.GetCurrent().Name);

        // Impersonate as Administrator user of the current computer.
        Impersonation imperson = new Impersonation();
        imperson.LogonAs("Administrator", null, "secret");

        // Display the current domain\user name (such as "JohnComputer\Administrator").
        Console.WriteLine(WindowsIdentity.GetCurrent().Name);

        // Revert back to the original user account.
        imperson.Logoff();

        // Display the current domain\user name (such as "OurCompany\JohnDoe").
        Console.WriteLine(WindowsIdentity.GetCurrent().Name);
    }
}
See Also