我在Azure VM和Web作业上的Azure虚拟机上共享了位置。我在webjob上部署了C#脚本来访问共享位置。当我执行Web作业来访问它时,出现了bewlo错误:
[03/22/2019 15:02:19> eb8046:SYS INFO]状态已更改为 初始化
[03/22/2019 15:02:29> eb8046:SYS INFO]运行脚本 带有脚本宿主-'WindowsScriptHost'的'AccessSharedLocation.exe'
[03/22/2019 15:02:29> eb8046:SYS INFO]状态更改为“正在运行”
[03/22/2019 15:02:32> eb8046:错误]
[03/22/2019 15:02:32> eb8046:ERR]未处理的异常: System.Net.WebException:访问路径 '\ 192.168.1.4 \ shared \ nilo.txt'被拒绝。 -> System.Net.WebException:访问路径 '\ 192.168.1.4 \ shared \ nilo.txt'被拒绝。 -> System.UnauthorizedAccessException:访问路径 '\ 192.168.1.4 \ shared \ nilo.txt'被拒绝。
[03/22/2019 15:02:32> eb8046:ERR]在 System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
[03/22/2019 15:02:32> eb8046:ERR]在 System.IO.FileStream.CreateFileOpenHandle(FileMode模式,FileShare 共享,FileOptions选项)
[03/22/2019 15:02:32> eb8046:ERR]在 System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess 访问,FileShare共享,Int32 bufferSize,FileOptions选项)
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebResponse..ctor(FileWebRequest request,Uri uri, FileAccess访问,布尔值useAsync)
[03/22/2019 15:02:32> eb8046:ERR] ---内部异常结束 堆栈跟踪---
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebResponse..ctor(FileWebRequest request,Uri uri, FileAccess访问,布尔值useAsync)
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebRequest.CreateResponse()
[03/22/2019 15:02:32> eb8046:ERR] ---内部异常结束 堆栈跟踪---
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebRequest.CreateResponse()
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebRequest。<> c.b__59_0(Object s)
[03/22/2019 15:02:32> eb8046:ERR]在 System.Threading.Tasks.Task`1.InnerInvoke()
[03/22/2019 15:02:32> eb8046:ERR]在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态)
[03/22/2019 15:02:32> eb8046:ERR] ---堆栈跟踪结束于 引发异常的先前位置---
[03/22/2019 15:02:32> eb8046:ERR]在 System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
[03/22/2019 15:02:32> eb8046:ERR] ---堆栈跟踪结束于 引发异常的先前位置---
[03/22/2019 15:02:32> eb8046:ERR]在 System.Threading.Tasks.TaskToApm.End [TResult](IAsyncResult asyncResult)
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult)
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.FileWebRequest.GetResponse()
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.WebClient.GetWebResponse(WebRequest请求)
[03/22/2019 15:02:32> eb8046:ERR]在 System.Net.WebClient.OpenRead(Uri地址)
[03/22/2019 15:02:32> eb8046:ERR]在 中的AccessSharedLocation.Program.buttonDownloadFile_Click() C:\ Users \ dharti.sutariya \ source \ repos \ AccessSharedLocation \ AccessSharedLocation \ Program.cs:line 63
[03/22/2019 15:02:32> eb8046:ERR]在 AccessSharedLocation.Program.Main(String [] args)在 C:\ Users \ dharti.sutariya \ source \ repos \ AccessSharedLocation \ AccessSharedLocation \ Program.cs:line 48
[03/22/2019 15:02:32> eb8046:INFO]世界您好!
[03/22/2019 15:02:32> eb8046:SYS INFO]状态已更改为“失败”
[03/22/2019 15:02:32> eb8046:SYS ERR]由于退出代码,作业失败 -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不允许安装/执行具有管理员访问权限的多个模块。相同的Powershell可以在我的本地系统上使用,但不能通过Azure Web作业使用。
答案 0 :(得分:2)
App Service沙箱明确不允许访问SMB协议(137/138/139/445)所需的端口。
本文在“受限出站端口”下对此进行了提及:
https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox。