mirror of https://github.com/dahall/Vanara.git
Added test class to impersonate a local acct.
parent
d0fc6a0f70
commit
e7d92752e7
|
@ -0,0 +1,23 @@
|
|||
using static Vanara.PInvoke.AdvApi32;
|
||||
|
||||
namespace Vanara.PInvoke.Tests;
|
||||
|
||||
public class ImpersonatedUser : IDisposable
|
||||
{
|
||||
private readonly SafeHTOKEN hToken;
|
||||
|
||||
public ImpersonatedUser(string un, string pwd, string? svr = null)
|
||||
{
|
||||
if (!(svr?.StartsWith('\\') ?? false))
|
||||
svr = $@"\\{svr}";
|
||||
Win32Error.ThrowLastErrorIfFalse(LogonUser(un, svr, pwd, LogonUserType.LOGON32_LOGON_NETWORK, LogonUserProvider.LOGON32_PROVIDER_DEFAULT, out hToken));
|
||||
Win32Error.ThrowLastErrorIfFalse(ImpersonateLoggedOnUser(hToken));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
RevertToSelf();
|
||||
((IDisposable)hToken).Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue