是否有某种方法可以作为本地(非网络)用户进行身份验证,以便在.Net中通过网络复制文件?
净使用不是一种选择,我似乎无法让LogonUser工作.
有任何想法吗?
[编辑]这是一些代码:
public class UserImpersonator : IDisposable { private WindowsImpersonationContext _impersonationContext; private IntPtr _userHandle = IntPtr.Zero; [DllImport("advapi32.dll",SetLastError = true)] private static extern bool logonUser( string lpszUsername,string lpszDomain,string lpszPassword,int dwlogonType,int dwlogonProvider,out IntPtr phToken ); [DllImport("kernel32.dll",SetLastError = true)] private static extern bool CloseHandle(IntPtr hHandle); public UserImpersonator(string username,string password) { logonUser(username,"",password,(int)logonType.logoN32_logoN_NETWORK,(int)logonProvider.logoN32_PROVIDER_DEFAULT,out _userHandle); _impersonationContext = WindowsIdentity.Impersonate(_userHandle); } public void Dispose() { CloseHandle(_userHandle); _impersonationContext.Undo(); } private enum logonType : int { logoN32_logoN_INTERACTIVE = 2,logoN32_logoN_NETWORK = 3,logoN32_logoN_BATCH = 4,logoN32_logoN_SERVICE = 5,logoN32_logoN_UNLOCK = 7,logoN32_logoN_NETWORK_CLEARTEXT = 8,logoN32_logoN_NEW_CREDENTIALS = 9,} private enum logonProvider { logoN32_PROVIDER_DEFAULT = 0,} }
当我使用(新的UserImpersonator(用户名,密码))包装File.Copy操作时,我得到:
System.IO.IOException: logon failure: unknown user name or bad password.
但是,如果我首先尝试连接到资源管理器中的共享(在请求时输入身份验证信息),则File.Copy可以正常工作.看来上面的代码根本没有做任何事情.
解决方法
您可以将WNetUseConnection与p / invokes一起使用.
看到这个帖子:
Accessing a Shared File (UNC) From a Remote,Non-Trusted Domain With Credentials