某些背景: 目前,我们在托管合作伙伴托管的FTP服务器上接收来自多个数据供应商的文件。作为新项目的一部分,我们正在设置Azure功能。此功能在我们的托管合作伙伴为VPN /专用网络访问设置的资源组中运行。此功能是使用Azure函数替换Excel / VBA中的多个旧程序的第一步。
我们需要的是将文件从FTP服务器移动到另一个内部(文件)服务器(以支持某些旧版程序)。 FTP服务器位于DMZ中,因此不像文件服务器那样属于域的一部分。
现在,我已经在Google周围搜索了数小时,找到了解决方案,并相信我已经使用https://stackoverflow.com/a/295703/998791和https://stackoverflow.com/a/1197430/998791
找到了解决方案public sealed class NetworkConnection : IDisposable
{
private string _uncShare;
public NetworkConnection(string uncShare, NetworkCredential credentials)
{
var nr = new Native.NETRESOURCE
{
dwType = Native.RESOURCETYPE_DISK,
lpRemoteName = uncShare
};
var userName = string.IsNullOrEmpty(credentials.Domain) ? credentials.UserName : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
int result = Native.WNetUseConnection(IntPtr.Zero, nr, credentials.Password, userName, 0, null, null, null);
if (result != Native.NO_ERROR)
{
throw new Win32Exception(result);
}
_uncShare = uncShare;
}
public void Dispose()
{
if (!string.IsNullOrEmpty(_uncShare))
{
Native.WNetCancelConnection2(_uncShare, Native.CONNECT_UPDATE_PROFILE, false);
_uncShare = null;
}
}
private class Native
{
public const int RESOURCETYPE_DISK = 0x00000001;
public const int CONNECT_UPDATE_PROFILE = 0x00000001;
public const int NO_ERROR = 0;
[DllImport("mpr.dll")]
public static extern int WNetUseConnection(IntPtr hwndOwner, NETRESOURCE lpNetResource, string lpPassword, string lpUserID,
int dwFlags, string lpAccessName, string lpBufferSize, string lpResult);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);
[StructLayout(LayoutKind.Sequential)]
public class NETRESOURCE
{
public int dwScope = 0;
public int dwType = 0;
public int dwDisplayType = 0;
public int dwUsage = 0;
public string lpLocalName = "";
public string lpRemoteName = "";
public string lpComment = "";
public string lpProvider = "";
}
}
}
用法:
using (new NetworkConnection(ftpServerSettings.UNCPath, new NetworkCredential(ftpServerSettings.UserName, ftpServerSettings.Password, ftpServerSettings.Domain)))
{
using (new NetworkConnection(fileServerSettings.UNCPath, new NetworkCredential(fileServerSettings.UserName, fileServerSettings.Password, fileServerSettings.Domain)))
{
handler.HandleFolders(bankDataRepository.GetFolderSettings());
}
}
在本地运行时,它工作正常,但是从Azure运行时,出现System.ComponentModel.Win32Exception,消息为“访问被拒绝”。
如果我需要FullTrust(我在某处见过此事)或服务器的权限是否有问题,我不确定Azure函数中是否允许DllImport。
有人可以启发我吗?
答案 0 :(得分:0)
很多Google搜索导致我发现了这个问题:Accessing Azure File Storage from Azure Function 链接到https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#restricted-outgoing-ports
它指出:
受限制的出港 无论地址如何,应用程序都无法使用端口445、137、138和139连接到任何地方。换句话说,即使连接到非私有IP地址或虚拟网络的地址,也要连接到端口445、137、138和445。 ,并且不允许139。
所以我们试图做的事是不可能的,并且与DllImport等无关。如果不尝试使用SMB,我想它可以很好地工作。
答案 1 :(得分:0)
尝试不在“隔离的”定价层中的应用程序服务或功能应用程序