2020-08-23 14:44:50 -04:00
|
|
|
#if NETFRAMEWORK
|
2017-11-27 13:11:20 -05:00
|
|
|
using System.Security.Principal;
|
|
|
|
using static Vanara.PInvoke.AdvApi32;
|
|
|
|
|
|
|
|
namespace Vanara.Security.Principal
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-03-09 23:00:58 -05:00
|
|
|
/// Impersonation of a user. Allows to execute code under another user context. Please note that the account that instantiates this class
|
|
|
|
/// needs to have the 'Act as part of operating system' privilege set.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <code>
|
|
|
|
/// // The following code impersonates an account to perform work
|
|
|
|
/// using (new WindowsImpersonatedIdentity("bob", "WORKDOMAIN", "bobs_secret_passw0rd")
|
|
|
|
/// {
|
2019-03-09 23:00:58 -05:00
|
|
|
/// // Perform impersonated work in the body. Once the 'using' statement closes,
|
|
|
|
/// // the impersonation ends.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// }
|
|
|
|
/// </code>
|
|
|
|
/// </remarks>
|
2019-03-09 23:00:58 -05:00
|
|
|
public class WindowsImpersonatedIdentity : WindowsLoggedInIdentity
|
2017-11-27 13:11:20 -05:00
|
|
|
{
|
|
|
|
private readonly WindowsImpersonationContext impersonationContext;
|
|
|
|
|
|
|
|
/// <summary>
|
2019-03-09 23:00:58 -05:00
|
|
|
/// Starts the impersonation with the given credentials. Please note that the account that instantiates this class needs to have the
|
|
|
|
/// 'Act as part of operating system' privilege set.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="userName">
|
2019-03-09 23:00:58 -05:00
|
|
|
/// A string that specifies the name of the user. This is the name of the user account to log on to. If you use the user principal
|
|
|
|
/// name (UPN) format, User@DNSDomainName, the <paramref name="domainName"/> parameter must be NULL.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </param>
|
|
|
|
/// <param name="domainName">
|
2019-03-09 23:00:58 -05:00
|
|
|
/// A string that specifies the name of the domain or server whose account database contains the <paramref name="userName"/> account.
|
|
|
|
/// If this parameter is NULL, the user name must be specified in UPN format. If this parameter is ".", the account is validated by
|
|
|
|
/// using only the local account database.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </param>
|
2019-03-09 23:00:58 -05:00
|
|
|
/// <param name="password">A string that specifies the plain-text password for the user account specified by <paramref name="userName"/>.</param>
|
2017-11-27 13:11:20 -05:00
|
|
|
/// <param name="logonType">
|
2019-03-09 23:00:58 -05:00
|
|
|
/// Type of the logon. This parameter can usually be left as the default. For more information, lookup more detail for the
|
|
|
|
/// dwLogonType parameter of the Windows LogonUser function.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </param>
|
|
|
|
/// <param name="provider">
|
2019-03-09 23:00:58 -05:00
|
|
|
/// The logon provider. This parameter can usually be left as the default. For more information, lookup more detail for the
|
|
|
|
/// dwLogonProvider parameter of the Windows LogonUser function.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </param>
|
|
|
|
public WindowsImpersonatedIdentity(string userName, string domainName, string password, LogonUserType logonType = LogonUserType.LOGON32_LOGON_INTERACTIVE,
|
2020-08-23 14:44:50 -04:00
|
|
|
LogonUserProvider provider = LogonUserProvider.LOGON32_PROVIDER_DEFAULT) : base(userName, domainName, password, logonType, provider) => impersonationContext = AuthenticatedIdentity?.Impersonate();
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
|
|
/// <summary>
|
2019-03-09 23:00:58 -05:00
|
|
|
/// Starts the impersonation with the given <see cref="WindowsIdentity"/>. Please note that the account that instantiates this class
|
|
|
|
/// needs to have the 'Act as part of operating system' privilege set.
|
2017-11-27 13:11:20 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="identityToImpersonate">The identity to impersonate.</param>
|
2020-08-23 14:44:50 -04:00
|
|
|
public WindowsImpersonatedIdentity(WindowsIdentity identityToImpersonate) : base(identityToImpersonate) => impersonationContext = AuthenticatedIdentity?.Impersonate();
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
|
|
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
2019-03-09 23:00:58 -05:00
|
|
|
public override void Dispose()
|
2017-11-27 13:11:20 -05:00
|
|
|
{
|
|
|
|
impersonationContext?.Undo();
|
2019-03-09 23:00:58 -05:00
|
|
|
base.Dispose();
|
2017-11-27 13:11:20 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 18:50:18 -05:00
|
|
|
}
|
|
|
|
#endif
|