December 16, 2009

Impersonator.cs

#region Namespaces
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
#endregion

namespace WF
{
public class Impersonator : IDisposable
{
#region Declarations
private WindowsImpersonationContext _impersonatedUser = null;
private IntPtr _userHandle;
#endregion

#region Impersonator
public Impersonator()
{
_userHandle = new IntPtr(0);
string user = ConfigurationSettings.AppSettings["StrAdmin"].ToString();
string userDomain = ConfigurationSettings.AppSettings["StrDomain"].ToString();
string password = ConfigurationSettings.AppSettings["StrAdminPswd"].ToString();

bool returnValue = LogonUser(user, userDomain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref _userHandle);
if (!returnValue)
throw new ApplicationException("Could not impersonate user");
WindowsIdentity newId = new WindowsIdentity(_userHandle);
_impersonatedUser = newId.Impersonate();
}
#endregion

#region IDisposable Members

public void Dispose()
{
if (_impersonatedUser != null)
{
_impersonatedUser.Undo();
CloseHandle(_userHandle);
}
}

#endregion

#region Interop imports/constants
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_LOGON_SERVICE = 3;
public const int LOGON32_PROVIDER_DEFAULT = 0;

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern bool LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
#endregion
}
}

No comments:

SonarQube with Jenkins Setup using Docker Images

https://funnelgarden.com/sonarqube-jenkins-docker/  https://medium.com/@hakdogan/an-end-to-end-tutorial-to-continuous-integration-and-con...