How to pass credentials during writing file at server path?

后端 未结 1 1866
温柔的废话
温柔的废话 2021-01-27 04:31

I would like to write a file at server path but when I tried to do that we got exception that we have no permission to do that. We have an application ID and password who has pe

1条回答
  •  梦毁少年i
    2021-01-27 04:47

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

    Example:

    IntPtr userToken = IntPtr.Zero;
    
    bool success = External.LogonUser(
      "john.doe", 
      "domain.com", 
      "MyPassword", 
      (int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2
      (int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0
      out userToken);
    
    if (!success)
    {
      throw new SecurityException("Logon user failed");
    }
    
    using (WindowsIdentity.Impersonate(userToken))
    {
      // put your code here
    }
    

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