c# – 使用.NET中的不同用户凭据对共享Windows文件夹进行RW访问
作者:互联网
我们正在使用Windows网络(AD正在使用中)
我们有用户共享的文件夹(仅限此用户访问)用户凭据已知
我需要在我的应用内访问该共享.
注意我已经读过有关模拟但我可以做的是在新用户上下文中打开整个应用程序(但我需要的是当前登录的用户,只是代表另一个用户访问Windows的共享文件夹)
可能吗?一段代码赞赏..
解决方法:
我终于成功了,对我有害!
对于那些感兴趣的人 – 请找到完成工作的示例方法(注意你需要System.Security.Principal Interop,还需要添加一些API静态方法)
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
public bool ImpersonateUser( string userName, string domain, string password ) {
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf ()) {
if (LogonUserA ( userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token ) != 0) {
if (DuplicateToken ( token, 2, ref tokenDuplicate ) != 0) {
tempWindowsIdentity = new WindowsIdentity ( tokenDuplicate );
impersonationContext = tempWindowsIdentity.Impersonate ();
if (impersonationContext != null) {
CloseHandle ( token );
CloseHandle ( tokenDuplicate );
return true;
}
}
}
}
if (token!= IntPtr.Zero)
CloseHandle ( token );
if (tokenDuplicate!=IntPtr.Zero)
CloseHandle ( tokenDuplicate );
return false;
}
标签:c,net-3-5,active-directory,impersonation 来源: https://codeday.me/bug/20190701/1342742.html