1

I'm really struggling with saving data to my local network NAS (a Synology DS214 if that matters).

I need to store some files in my network folders after creating them in another part of my program, but I haven't been able to handle the authentication/permissions properly.

My code atm is this:

WrapperImpersonationContext WIContext = 
    new WrapperImpersonationContext("\\\\DiskStation", "admin", "admin");

try
{
    WIContext.Enter();

    // code to select the final path simplified.
    string fileName = "file.txt"; 
    string originalPath = Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments);
    originalPath= Path.Combine(new string[] {originalPath, fileName});
    string finalPath = "\\\\DiskStation\\Virtual\\DestFolder";

    if (!Directory.Exists(finalPath))
    {
        // This goes well for whatever reason
        Directory.CreateDirectory(finalPath);
    }
    finalPath = Path.Combine(new string[] {finalPath, fileName});

    // This fails for wrong username/password
    File.Move(originalPath, finalPath);

} catch (Exception ex)
{
    // Exception showing simplified here
    MessageBox.Show(ex.ToString());
    throw;
} finally
{
    WIContext.Leave();
}

The code used for the WrapperImpersonationContext I found here: WindowsImpersonationContext made easy

As written in my code when I try to move the file I get an UnauthorizedAccessException: Access to the path is denied. I also tried to create a new file in the network folder with the same results.

While looking at the Michiel Vanotegem's code linked above, I discovered that I get an authentication error calling the LogonUser function (error code 1326 that gets me a Win32Exception (0x80004005): The user name or password is incorrect).

I tried to use the WNetUseConnection function looking at this and this pages but while I get no error from the function (after substituting it in the Michiel code), when I try to move the file I get the same UnauthorizedAccessException: Access to the path is denied.

I also tried to fiddle with the domain passed to the Impersonation Wrapper but I couldn't seem to make it work. I feel like I'm missing something... Can someone kindly point me to the right direction or help me with this issue?

Ty all who contributes in advance.

Edit 15/12/2017 11:52: I discovered that if I try to rerun the LogonUser function immediately after the first error I get a different exception (error 87 Win32Exception (0x80004005): The parameter is incorrect)

Forna
  • 149
  • 3
  • 13
  • go back to basics. Can you save a file their outside the program (just from your PC?). Are you using Active Directory, Is admin with a password of admin valid? – bilpor Dec 14 '17 at 11:54
  • Yes i can programmatically create and move around a file inside my pc, I don't think we have any active directory in our local network and the NAS doesn't belong to any domain , the user/password are the ones I use to connect to the remote folders using the windows file explorer and they works (I also can create, read, update and delete files in the same folders I try to modify from my code) – Forna Dec 14 '17 at 13:20
  • Hmm, it is rather odd that you can create a directory but not move a file into it. That does not sound like a logon problem. Consider the unintuitive reason, the account you use does not have access to the *source file*. And why would it. Especially tricky since File.Move() also requires delete rights. – Hans Passant Dec 14 '17 at 13:35
  • Yeah that's quite tricky, I forgot to mention that the files I move are generated by the same program, this is only the code I wanted to use to move them in the correct location. – Forna Dec 14 '17 at 14:09
  • 1
    The wrapper you are using is quite old.. have you tried a different approach, for example using [WNetAddConnection2](https://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share)? – Lennart Stoop Dec 20 '17 at 15:55

1 Answers1

3

I followed on @LennartStoop suggestion, so I enclosed my code in a using block instead of a try finally using the code I borrowed from this answer:

using (NetworkConnection netConn = 
    new NetworkConnection("\\\\DiskStation", new NetworkCredential("admin", "admin")))
{
    // My code here
}

Using this I've been able to establish a connection the network folder and perform all the IO operation I needed so ty very much for the tip Lennart :)

Forna
  • 149
  • 3
  • 13