MailBee.NET Objects 7.1

Impersonation Class

Provides access to the impersonation functions.

For a list of all members of this type, see Impersonation Members.

System.Object
   MailBee.Security.Impersonation

public class Impersonation : IDisposable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

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   To use this class, make sure MailBee.NET Security Powerup is licensed (see Powerup class for details).

Example

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.

[C#]
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);
    }
}
[Visual Basic]
Imports System
Imports System.Security.Principal

Imports MailBee
Imports MailBee.Security

Class Sample
    Shared Sub Main(ByVal args() As String)
        ' Display the current domain\user name (such as "OurCompany\JohnDoe").
        Console.WriteLine(WindowsIdentity.GetCurrent().Name)

        ' Impersonate as Administrator user of the current computer.
        Dim imperson As Impersonation = New Impersonation
        imperson.LogonAs("Administrator", Nothing, "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)
    End Sub
End Class

Requirements

Namespace: MailBee.Security

Assembly: MailBee.NET (in MailBee.NET.dll)

See Also

Impersonation Members | MailBee.Security Namespace