0

I have put endpoint that accept the zip file. The endpoint is working fine to get the zip file. Now from that endpoint after I get that file I am trying to copy that file to a different remote Server. I try below code to connect to remote Server using the below code by referring to micrsoft docs https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=net-6.0:

  bool returnValue = LogonUser(@"ACC\Test.test", "acc.local", "password",
                    9, 0,
                    out safeAccessTokenHandle);

                if (returnValue)
                {
                    WindowsIdentity.RunImpersonated(safeAccessTokenHandle, () =>
                    {

                         //Destination remote server folder to upload file
                        var pathToUploadFile = @"\\152.158.100.45\D\FileUpload";
                        string fileName;

                        fileName = file.FileName;
                        var path = Path.Combine(pathToUploadFile, fileName);

                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            //save a zip file to folder
                            file.CopyTo(stream);
                        }
                    });
                }

In this code I get error of username and password is incorrect in this line of code:

var stream = new FileStream(path, FileMode.Create)

I am not even sure if the LogonUser method connects to a remote server. So is there is better way to do this copy file to remote server Please give an idea to implement in .net core 6.0 C#. Any help is really appreciated. Thanks

Sabbu
  • 169
  • 1
  • 2
  • 15
  • maybe map remote location to a local drive, and copy? – urlreader Mar 25 '22 at 18:34
  • Seems like giving that kind of access would be pretty dangerous. Couldn't you just post it to the other server and let its backend decide where to put it? – pcalkins Mar 25 '22 at 20:19
  • @urlreader how to do that and how it solve my issue. – Sabbu Mar 26 '22 at 01:19
  • in general people don't have access to write to or modify folders on machines outside of a domain. For "remote" servers, generally there'd be an API which exposes methods for uploading files. Then the back-end would check authorization and then process the file. (It would have permission to write/modify files with in it's own domain...) – pcalkins Mar 28 '22 at 18:49

1 Answers1

0

Finally I am able to solve the issue by implementing the Win32 API called WNetUseConnection solutions that was mentioned on below questions: Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials

Sabbu
  • 169
  • 1
  • 2
  • 15
  • This is also another solution. I am adding here so that in future it might help somebody. https://thoughtsonprogramming.wordpress.com/2018/09/28/how-to-securely-access-remote-machine-or-server-from-your-asp-net-mvc-website/ – Sabbu Mar 29 '22 at 14:09