Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SecureString password support #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions SimpleImpersonation/Impersonation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;

Expand All @@ -8,28 +9,59 @@ namespace SimpleImpersonation
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public sealed class Impersonation : IDisposable
{
private readonly SafeTokenHandle _handle;
private readonly WindowsImpersonationContext _context;
private SafeTokenHandle _handle;
private WindowsImpersonationContext _context;

public static Impersonation LogonUser(string domain, string username, string password, LogonType logonType)
{
return new Impersonation(domain, username, password, logonType);
}

private Impersonation(string domain, string username, string password, LogonType logonType)
public static Impersonation LogonUser(string domain, string username, SecureString password, LogonType logonType)
{
return new Impersonation(domain, username, password, logonType);
}

private void CompleteImpersonation(bool ok, IntPtr handle)
{
IntPtr handle;
var ok = NativeMethods.LogonUser(username, domain, password, (int)logonType, 0, out handle);
if (!ok)
{
var errorCode = Marshal.GetLastWin32Error();

if (handle != IntPtr.Zero)
NativeMethods.CloseHandle(handle);

throw new ApplicationException(string.Format("Could not impersonate the elevated user. LogonUser returned error code {0}.", errorCode));
}

_handle = new SafeTokenHandle(handle);
_context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle());
}

private Impersonation(string domain, string username, SecureString password, LogonType logonType)
{
IntPtr handle;
var passPtr = Marshal.SecureStringToGlobalAllocUnicode(password);
bool ok;
try
{
ok = NativeMethods.LogonUser(username, domain, passPtr, (int)logonType, 0, out handle);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(passPtr);
}

CompleteImpersonation(ok, handle);
}

private Impersonation(string domain, string username, string password, LogonType logonType)
{
IntPtr handle;
var ok = NativeMethods.LogonUser(username, domain, password, (int)logonType, 0, out handle);
CompleteImpersonation(ok, handle);
}

public void Dispose()
{
_context.Dispose();
Expand Down
3 changes: 3 additions & 0 deletions SimpleImpersonation/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ internal class NativeMethods
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool LogonUser(String lpszUsername, String lpszDomain, IntPtr phPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
Expand Down
8 changes: 4 additions & 4 deletions SimpleImpersonation/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
[assembly: AssemblyDescription("A tiny library that lets you impersonate any user, by acting as a managed wrapper for the LogonUser Win32 function.")]
[assembly: AssemblyCompany("Matt Johnson")]
[assembly: AssemblyProduct("Simple Impersonation")]
[assembly: AssemblyCopyright("Copyright © 2013, Matt Johnson")]
[assembly: AssemblyCopyright("Copyright © 2015, Matt Johnson")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.1.2")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0")]