CEL_Payroll/Payroll.BO/Common/FileAccessWithImpersonation.cs
2024-09-17 14:30:13 +06:00

145 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.IO;
namespace Payroll.BO
{
[Serializable]
public class FileAccessWithImpersonation
{
#region Declaration
string userName = string.Empty;// "chapal" ;
string domain = string.Empty;// "CEL";
string password = string.Empty;// "cpl!@#45";
string target = string.Empty;// @"\\CEL02\d$\Images\";
/// <summary>
/// This is 9 if There is No Domain Defined and 2 otherwise
/// </summary>
public const int LOGON32_LOGON_INTERACTIVE = 9; //2;
/// <summary>
/// This is 3 if There is No Domain Defined and 0 otherwise
/// </summary>
public const int LOGON32_PROVIDER_DEFAULT = 3;//0;
WindowsImpersonationContext impersonationContext;
#endregion
#region Functions
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
private bool impersonateValidUser()
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
domain = Ease.CoreV35.Utility.ConfigUtility.GetAppSettings("ADDomain");
userName = Ease.CoreV35.Utility.ConfigUtility.GetAppSettings("ADUser");
password = Ease.CoreV35.Utility.ConfigUtility.GetAppSettings("ADPassword");
password = Ease.CoreV35.Utility.Global.CipherFunctions.Decrypt("Cel.Admin", password);
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
private void undoImpersonation()
{
if(impersonationContext!=null)
impersonationContext.Undo();
}
public void UploadFile(string TargetFolder,string FullFilePath,string NewFileName)
{
//System.IO.FileStream fs = null;
//fs = System.IO.File.Open(FullFilePath , System.IO.FileMode.Open);
//byte[] bytes = new byte[fs.Length];
//fs.Read(bytes , 0 , Convert.ToInt32(fs.Length));
byte[] bytes = File.ReadAllBytes(FullFilePath);
try
{
if (impersonateValidUser())
{
//string[] Files = Directory.GetFiles(TargetFolder);
string fullTargetPath = TargetFolder + "\\" + NewFileName;
File.WriteAllBytes(fullTargetPath, bytes);
}
}
finally
{
undoImpersonation();
}
}
public byte[] GetFileInBytes(string SourceFolder, string FileName)
{
byte[] bytes = null;
try
{
if (impersonateValidUser())
{
string fullSourcePath = SourceFolder+ "\\" + FileName;
bytes = File.ReadAllBytes(fullSourcePath);
}
}
finally
{
undoImpersonation();
}
return bytes;
}
#endregion
}
}