问题描述
我有一个在我的表单应用中使用Cassia库的函数,该函数可以将通过rdp登录的所有用户返回到会话主机以及有关其会话的一些详细信息(然后将其添加到datagridview)。
只要您在域中,它就可以正常运行,。
但是我想在域外运行此功能。假冒似乎是一种解决方法,但我遇到了问题,我得到了“用户名或密码错误”(当我100%确信它们正确时),或者出现了“访问被拒绝”错误从决明子功能。这意味着模拟没有引发错误,但也没有起作用。
我已经看到一些地方提到必须在要连接的计算机上将注册表代码AllowRemoteRPC设置为1。
代码(主要从其他地方复制):
string serverAddr = "server.com";
string userName = "domainUser"
string uPw = "1234"
IntPtr token;
if (!NativeMethods.logonUser(userName,serverAddr,uPw,NativeMethods.logonType.NewCredentials,NativeMethods.logonProvider.WinNT50,out token))
{
throw new Win32Exception();
}
try
{
IntPtr tokenDuplicate;
if (!NativeMethods.Duplicatetoken(token,NativeMethods.SecurityImpersonationLevel.Impersonation,out tokenDuplicate))
{
throw new Win32Exception();
}
try
{
using (WindowsImpersonationContext impersonationContext = new WindowsIdentity(tokenDuplicate).Impersonate())
{
#Do stuff here querying the terminal servers
ITerminalServicesManager manager = new TerminalServicesManager();
IList<ITerminalServer> svs = manager.GetServers("DOMAIN");
int rowCnt = 0;
foreach (ITerminalServer server in svs)
{
server.open();
string svr = default(string);
string cName = default(string);
string state = default(string);
string uName = default(string);
foreach (ITerminalServicesSession session in server.GetSessions())
{
NTAccount account = session.UserAccount;
uName = session.UserName;
svr = session.Server.ServerName;
cName = session.ClientName;
state = session.ConnectionState.ToString();
if (account != null)
{
userGrid1.Rows.Add();
userGrid1.Rows[rowCnt].Cells[0].Value = uName;
userGrid1.Rows[rowCnt].Cells[1].Value = svr;
userGrid1.Rows[rowCnt].Cells[2].Value = cName;
userGrid1.Rows[rowCnt].Cells[3].Value = state;
rowCnt++;
}
}
}
impersonationContext.Undo();
}
}
finally
{
if (tokenDuplicate != IntPtr.Zero)
{
NativeMethods.CloseHandle(tokenDuplicate);
}
}
}
finally
{
if (token != IntPtr.Zero)
{
NativeMethods.CloseHandle(token);
}
}
}
#the above is all inside a function,NativeMethods class copied below for clarity;
internal static class NativeMethods
{
internal enum logonType : int
{
Interactive = 2,Network = 3,Batch = 4,Service = 5,Unlock = 7,NetworkCleartext = 8,NewCredentials = 9
}
internal enum logonProvider : int
{
Default = 0,WinNT35 = 1,WinNT40 = 2,WinNT50 = 3
}
internal enum SecurityImpersonationLevel : int
{
Anonymous = 0,Identification = 1,Impersonation = 2,Delegation = 3
}
[DllImport("advapi32.dll",CharSet = CharSet.Unicode,SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool logonUser(
string userName,string domain,string password,logonType logonType,logonProvider logonProvider,out IntPtr token);
[DllImport("advapi32.dll",SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool Duplicatetoken(
IntPtr existingTokenHandle,SecurityImpersonationLevel securityImpersonationLevel,out IntPtr duplicatetokenHandle);
[DllImport("kernel32.dll",SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr handle);
}
冒名顶替对我来说是很新的,因此可能会引起我的基本误会,或者仅仅是语法问题。我不知道。任何帮助将不胜感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)