Azure Web 作业在访问共享位置时被拒绝访问

Azure web job is getting access denied while accessing shared location

我在 Azure 的同一个 Vnet 中共享了 Azure VM 和 Web 作业上的位置。我在 webjob 上部署了 C# 脚本以访问共享位置。当我执行 web 作业来访问它时,出现 bewlo 错误:

[03/22/2019 15:02:19 > eb8046: SYS INFO] Status changed to Initializing

[03/22/2019 15:02:29 > eb8046: SYS INFO] Run script 'AccessSharedLocation.exe' with script host - 'WindowsScriptHost'

[03/22/2019 15:02:29 > eb8046: SYS INFO] Status changed to Running

[03/22/2019 15:02:32 > eb8046: ERR ]

[03/22/2019 15:02:32 > eb8046: ERR ] Unhandled Exception: System.Net.WebException: Access to the path '2.168.1.4\shared\nilo.txt' is denied. ---> System.Net.WebException: Access to the path '2.168.1.4\shared\nilo.txt' is denied. ---> System.UnauthorizedAccessException: Access to the path '2.168.1.4\shared\nilo.txt' is denied.

[03/22/2019 15:02:32 > eb8046: ERR ] at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean useAsync)

[03/22/2019 15:02:32 > eb8046: ERR ] --- End of inner exception stack trace ---

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean useAsync)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebRequest.CreateResponse()

[03/22/2019 15:02:32 > eb8046: ERR ] --- End of inner exception stack trace ---

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebRequest.CreateResponse()

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebRequest.<>c.b__59_0(Object s)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Threading.Tasks.Task`1.InnerInvoke()

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

[03/22/2019 15:02:32 > eb8046: ERR ] --- End of stack trace from previous location where exception was thrown ---

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)

[03/22/2019 15:02:32 > eb8046: ERR ] --- End of stack trace from previous location where exception was thrown ---

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Threading.Tasks.TaskToApm.End[TResult](IAsyncResult asyncResult)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.FileWebRequest.GetResponse()

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.WebClient.GetWebResponse(WebRequest request)

[03/22/2019 15:02:32 > eb8046: ERR ] at System.Net.WebClient.OpenRead(Uri address)

[03/22/2019 15:02:32 > eb8046: ERR ] at AccessSharedLocation.Program.buttonDownloadFile_Click() in C:\Users\dharti.sutariya\source\repos\AccessSharedLocation\AccessSharedLocation\Program.cs:line 63

[03/22/2019 15:02:32 > eb8046: ERR ] at AccessSharedLocation.Program.Main(String[] args) in C:\Users\dharti.sutariya\source\repos\AccessSharedLocation\AccessSharedLocation\Program.cs:line 48

[03/22/2019 15:02:32 > eb8046: INFO] Hello World!

[03/22/2019 15:02:32 > eb8046: SYS INFO] Status changed to Failed

[03/22/2019 15:02:32 > eb8046: SYS ERR ] Job failed due to exit code -532462766

下面是我的C#代码

using System;

using System.ComponentModel;

using System.Net;

using System.Runtime.InteropServices;



namespace AccessSharedLocation

{

    public class NetworkConnection : IDisposable

    {

        readonly string _networkName;



        public NetworkConnection(string networkName, NetworkCredential credentials)

        {

            _networkName = networkName;



            var netResource = new NetResource

            {

                Scope = ResourceScope.GlobalNetwork,

                ResourceType = ResourceType.Disk,

                DisplayType = ResourceDisplaytype.Share,

                RemoteName = networkName

            };



            var userName = string.IsNullOrEmpty(credentials.Domain)

                ? credentials.UserName

                : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);



            var result = WNetAddConnection2(

                netResource,

                credentials.Password,

                userName,

                0);



            if (result != 0)

            {

                throw new Win32Exception(result);

            }

        }



        ~NetworkConnection()

        {

            Dispose(false);

        }



        public void Dispose()

        {

            Dispose(true);

            GC.SuppressFinalize(this);

        }



        protected virtual void Dispose(bool disposing)

        {

            WNetCancelConnection2(_networkName, 0, true);

        }



        [DllImport("mpr.dll")]

        private static extern int WNetAddConnection2(NetResource netResource,

            string password, string username, int flags);



        [DllImport("mpr.dll")]

        private static extern int WNetCancelConnection2(string name, int flags,

            bool force);



        [StructLayout(LayoutKind.Sequential)]

        public class NetResource

        {

            public ResourceScope Scope;

            public ResourceType ResourceType;

            public ResourceDisplaytype DisplayType;

            public int Usage;

            public string LocalName;

            public string RemoteName;

            public string Comment;

            public string Provider;

        }



        public enum ResourceScope : int

        {

            Connected = 1,

            GlobalNetwork,

            Remembered,

            Recent,

            Context

        };



        public enum ResourceType : int

        {

            Any = 0,

            Disk = 1,

            Print = 2,

            Reserved = 8,

        }



        public enum ResourceDisplaytype : int

        {

            Generic = 0x0,

            Domain = 0x01,

            Server = 0x02,

            Share = 0x03,

            File = 0x04,

            Group = 0x05,

            Network = 0x06,

            Root = 0x07,

            Shareadmin = 0x08,

            Directory = 0x09,

            Tree = 0x0a,

            Ndscontainer = 0x0b

        }

    }

}

谁能帮我解决这个问题。任何帮助都会很棒。如果这也可以通过 Powershell 实现,那么它就可以工作。我已经尝试了几个 powershell,但 Azure 不允许 install/execute 几个具有管理员访问权限的模块。相同的 powershell 适用于我的本地系统,但不适用于 Azure 网络作业。

应用服务沙箱明确不允许访问 SMB 协议所需的端口 (137/138/139/445)。

本文在受限传出端口下提到它:

https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox.