Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Network Share from Raspberry Pi running IoT Core in UWP app

I have a c# UWP app that I'm intending to run on a Raspberry PI with Windows 10 IoT Core. The problem I have is when I try to connect to a UNC share to copy some files.

The network is just a home network with local user credentials, share is on another computer on the same network.

When running the app locally I can just use await StorageFolder.GetFolderFromPathAsync(@"\\share\folder"); to connect to the share and this works fine, I'm assuming this is because the credentials I'm using are saved on the local machine. When ran on the RPi the error received is: "The system cannot find the file specified."

Does anyone have any ideas on how I would connect to this drive, I'm game for anything at this stage to get it to work...

What I've tried:

  1. Share has permissions for everyone and can be accessed without credentials
  2. Network share computer firewall is off.
  3. manifest has the private networks, enterprise auth, and Internet (both) setup (assuming okay as works locally).
  4. await StorageFolder.GetFolderFromPathAsync(@"\\share\folder"); ("The system cannot find the file specified.")
  5. using powershell with net use "\\share\folder" "password" /USER:"user" works and unc can be browsed
  6. Tried using WNetAddConnection2 as in Prevent WNetAddConnection2 class which allows prohibited user to access shared folder
  7. Tried using WNetUseConnection with both user prompt and without (neither works)
  8. FolderPicker or FileOpenPicker but these seem to be disabled for IoT Core (https://ms-iot.github.io/content/en-US/win10/UnavailableApis.htm).

Thanks in advance,

Paul.

like image 277
Paul Charlton Avatar asked Oct 12 '15 21:10

Paul Charlton


People also ask

How do I connect my Raspberry Pi to Windows?

Download and Install the Windows 10 IoT Dashboard application on your host Windows 10 computer. Launch the dashboard application and select “set up a new device” to flash your Raspberry Pi. Insert the flashed MicroSD card into the Pi and power it up. Connect to the Pi remotely through the IoT Dashboard on your host PC.

Does Windows 10 IoT support Remote Desktop?

Turn on the remote display functionality on your Windows 10 IoT Core device. Connect your device to the Internet and connect to Windows Device Portal. Choose the page "Remote" from the options on the left, and mark the check box labeled "Enable Window IoT Remote Server".

Can I program Raspberry Pi in Windows?

Once you install it and run the Windows IoT Remote Control app, you can control and interact with the device remotely. Now, you can start developing for the Raspberry Pi using Windows 10.


1 Answers

Have you tried impersonation yet? Here is what I use in one of my projects:

[DllImport("advapi32.dll", SetLastError = true)]            
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

private void Impersonate(Enum domainName, string userName, string password)
{
    IntPtr _tokenHandle = IntPtr.Zero;
    int Logon32_Provider_Default = 0;
    int Logon32_Logon_Interactive = 2;

    bool userSuccess = LogonUser(userName, domainName.ToString(), password, Logon32_Logon_Interactive, Logon32_Provider_Default, ref _tokenHandle);

    if (!userSuccess)
    {
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    WindowsImpersonationContext _impersonatedUser = new WindowsIdentity(_tokenHandle).Impersonate();
}
like image 100
Chris Avatar answered Sep 24 '22 00:09

Chris