#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...
- 
http://sharepointmagazine.net/articles/creating-documents-with-sharepoint-designer-2010-workflows Issue#1 : The workflow could not cr...
- 
clear $site = Get-SPSite -Identity http://url $q = New-Object -TypeName microsoft.SharePoint.SPSiteDataQuery $q.Lists = "<List...
- 
I guess Drawing is my first extra curricular activity which i have developed during the school days. Thanks to Bhargavi Teacher[my Drawing T...
 
No comments:
Post a Comment