1

Getting access denied error when am trying to access shared network drive using UNC path from web job. Am trying to read a file from shared folder and SFTP on client-server. From local, it is working fine being able to access all files. But from azure web job getting access denied error to folder. Here is my code :

 var con = new ConnectionInfo(host, 22, username, methods.ToArray());
                using (var client = new SftpClient(con))
                {
                    Console.WriteLine("Connecting to client");
                    client.Connect();
                    Console.WriteLine("Connected to client");
                    Console.WriteLine("Getting file from Azure ");
                    string tstkey1 = File.ReadAllText(@"\\{shared folder}\test\test.txt");
                    Console.WriteLine("Connected");
                    string tstkey = File.ReadAllText(@"\\{shared folder}\test\test.txt");
                    Console.WriteLine(tstkey);                  
                    byte[] fileContents = Encoding.UTF8.GetBytes(tstkey);
                    Stream requestStream = new MemoryStream(fileContents);
                    Console.WriteLine(requestStream);
                    Console.WriteLine("Uploading to FILE to client SFTP");
                    client.UploadFile(requestStream, "/inbound/STEIN366_ACH_1/*.*" + "Test");
                    client.Disconnect();
                    Console.WriteLine("Deleting File");
                    File.Delete(@"\\{folder}\test\test.txt");
                    Console.WriteLine("Dissconnected from client");
                }  

Can someone help me, how to connect to the shared folder from Azure! Thanks in advance.

ravi teja
  • 26
  • 3
  • 1
    Your question title says *"shared network drive"*. Your code is for SFTP. So what are you really asking about? + Show us your exact exception error message and stacktrace. – Martin Prikryl Nov 22 '18 at 08:28
  • @MartinPrikryl : Here is my code from where am trying to read the file from a shared drive. `string tstkey = File.ReadAllText(@"\\{Shared Drive}\test\test.txt"); Console.WriteLine(tstkey); byte[] fileContents = Encoding.UTF8.GetBytes(tstkey); Stream requestStream = new MemoryStream(fileContents);` – ravi teja Nov 26 '18 at 14:01
  • @MartinPrikryl : **Error Message**: **Error Message** : `[ERR ] Unhandled Exception: System.UnauthorizedAccessException: Access to the path '\\{Shared Folder}\test\test.txt' is denied. [ ERR ] at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) [ ERR ] at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)` – ravi teja Nov 26 '18 at 14:02
  • 1
    **Edit your question** to 1) Show only the relevant code (no SFTP). 2) Show the exception details 3) Explain why you believe that the WebJob should be able to access the UNC path + Remove the comments above. – Martin Prikryl Nov 26 '18 at 14:09

1 Answers1

1

If you want to transfer files with SFTP you could use WinSCP .NET Assembly.

Here is the guide to use WinSCP. And you can refer to this answer about WinSCP use.

If you still have questions, please let me know.

George Chen
  • 13,703
  • 2
  • 11
  • 26