public Stream Get(IActivityIOPath path, List<string> filesToCleanup)
{
Stream result;
if (!RequiresAuth(path))
{
if (File.Exists(path.Path))
{
result = new MemoryStream(File.ReadAllBytes(path.Path));
}
else
{
throw new Exception("File not found [ " + path.Path + " ]");
}
}
else
{
try
{
// handle UNC path
SafeTokenHandle safeTokenHandle;
string user = ExtractUserName(path);
string domain = ExtractDomain(path);
bool loginOk = LogonUser(user, domain, path.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);
if (loginOk)
{
using (safeTokenHandle)
{
WindowsIdentity newID = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
using (WindowsImpersonationContext impersonatedUser = newID.Impersonate())
{
// Do the operation here
result = new MemoryStream(File.ReadAllBytes(path.Path));
impersonatedUser.Undo(); // remove impersonation now
}
}
}
else
{
// login failed
throw new Exception("Failed to authenticate with user [ " + path.Username + " ] for resource [ " + path.Path + " ] ");
}
}
catch (Exception ex)
{
Dev2Logger.Log.Error(ex);
throw new Exception(ex.Message, ex);
}
}
return result;
}