Impersonation Class |
Namespace: MailBee.Security
The Impersonation type exposes the following members.
Name | Description | |
---|---|---|
Impersonation |
Creates an instance of Impersonation class.
| |
Impersonation(String) |
Creates and unlocks an instance of Impersonation class.
|
Name | Description | |
---|---|---|
Dispose |
Releases the resources associated with the Impersonation object.
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Logoff |
Terminates the impersonation mode initiated with LogonAs(String, String, String) call and reverts back to the user account the current thread was created on.
| |
LogonAs |
Makes the current thread impersonate the specified user account.
| |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
IsImpersonated |
Gets whether the impersonation mode is on and the current thread is being impersonating another user.
| |
LastResult |
Gets a numeric code of the last error.
| |
ThrowExceptions |
Gets or sets whether the object will throw exceptions on errors.
|
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 is licensed (MailBee.NET Objects full bundle does include the license). |
Note |
---|
This class is not available in .NET Standard 2.0 edition and in UWP edition (as it relies on Win32 API). |
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); } }