C# 纯.Net中带凭据的网络文件传输
最近,我创建了一个通过网络传输带有凭据的文件的方法。为此,我使用WIN-32API登录用户。LogonUser使用advapi32.dll。这限制了我们在部署web应用程序的操作系统中提供并支持此dllC# 纯.Net中带凭据的网络文件传输,c#,asp.net,networking,C#,Asp.net,Networking,最近,我创建了一个通过网络传输带有凭据的文件的方法。为此,我使用WIN-32API登录用户。LogonUser使用advapi32.dll。这限制了我们在部署web应用程序的操作系统中提供并支持此dll [System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)] public sta
[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
private static string CopyToNetwork(string _sourceFile, string _targetFile, string _domain, string _userName, string _password)
{
IntPtr tokenHandle = new IntPtr(0);
bool returnValue = LogonUser(_userName, _domain, _password, 9, 3, ref tokenHandle);
if(returnValue)
{
using(WindowsIdentity wid = new WindowsIdentity(tokenHandle))
{
using(WindowsImpersonationContext impersonatedUser = wid.Impersonate())
{
File.Copy(_sourceFile, _targetFile, IS_OVERWRITE);
}
}
}
}
现在,我们不想要这种依赖性。我们可以用纯.Net做同样的事情吗?这意味着不使用任何WIN API。在Windows advapi32.dll上始终存在,并且无论如何都不应该重新分发。我计划在Linux上托管web应用程序。