Dev2.PathOperations.Dev2FileSystemProvider.Get C# (CSharp) Method

Get() private method

private Get ( IActivityIOPath path, List filesToCleanup ) : Stream
path IActivityIOPath
filesToCleanup List
return Stream
        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;
        }

Usage Example

 public void Dev2FileSystemProvider_GetOperation_NonExistingPath_FriendlyError()
 {
     bool pass = false;
     var testProvider = new Dev2FileSystemProvider();
     IActivityIOPath path = ActivityIOFactory.CreatePathFromString("C:/dadsdascasxxxacvaawqf", false);
     try
     {
         using(testProvider.Get(path, new List<string>()))
         {
             // foo ;)
         }
     }
     catch(Exception ex)
     {
         Assert.AreEqual("File not found [ C:/dadsdascasxxxacvaawqf ]", ex.Message);
         pass = true;
     }
     if(!pass)
     {
         Assert.Fail("The corrrect error wasnt returned");
     }
 }