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

后端 未结 1 1203
星月不相逢
星月不相逢 2021-02-04 03:46

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.

<
1条回答
  •  -上瘾入骨i
    2021-02-04 04:27

    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();
    }
    

    0 讨论(0)
提交回复
热议问题