如何解决C#,Cassia终端服务器查询和模拟
我有一个在我的表单应用中使用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 举报,一经查实,本站将立刻删除。