I have shared location on Azure VM and web job in the same Vnet on Azure. I have C# script deployed on webjob to access the shared location. When I execute web job to access it, I am getting bewlo error:
[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 '\192.168.1.4\shared\nilo.txt' is denied. ---> System.Net.WebException: Access to the path '\192.168.1.4\shared\nilo.txt' is denied. ---> System.UnauthorizedAccessException: Access to the path '\192.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
Below is my C# code
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
}
}
}
Can anyone please help me out with this. Any help would be great. if this is achievable thorugh Powershell as well, it would work. I have tried several powershell but Azure does not allow to install/execute several module with admin access. The same powershell works with my local system but not through Azure web job.
The App Service sandbox explicitly does not allow access to the ports necessary for SMB protocol (137/138/139/445).
This article mentions it under Restricted Outgoing Ports:
https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox.