#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
}
}
December 16, 2009
Subscribe to:
Post Comments (Atom)
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...
-
The competitive examination for the Indian Forest Service is conducted by Union Public Service Commission (UPSC). IFS exam is conducted ann...
-
Happy New Year. May this year is the year for your dreams, passions, everything you believe, your wishes, your desires, your thoughts, your ...
-
Azure Mobile App Service provides services that allow you to support mobile apps across multiple platforms. It does not provide hosting ser...
No comments:
Post a Comment