當我執行此代碼,如何使用PrincipalContext關閉連接到遠程主機?
PrincipalContext oPrincipalContext = new PrincipalContext(
ContextType.Machine,
computer.Name,
null,
ContextOptions.Negotiate,
Settings.UserName,
Settings.UserPassword))
GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(
oPrincipalContext,
Settings.AdministratorsGroup);
連接到遠程機器被創建。我能夠看到它在cmd.exe中寫入「net use」。
但我不知道如何在關閉我的應用程序之前關閉此連接。
它會在我退出我的應用程序時自動關閉。
這裏是我的方法:
public Dictionary<Principal, ComputerPrincipal>
GetMembersOfAdministratorsGroup(ComputerPrincipal computer)
{
var usersList = new Dictionary<Principal, ComputerPrincipal>();
var tempUsersList = new Dictionary<string, Principal>();
using (PrincipalContext oPrincipalContext =
new PrincipalContext(
ContextType.Machine,
computer.Name,
null,
ContextOptions.Negotiate,
Settings.UserName,
Settings.UserPassword))
{
using (GroupPrincipal oGroupPrincipal =
GroupPrincipal.FindByIdentity(
oPrincipalContext,
Settings.AdministratorsGroup))
{
if (oGroupPrincipal != null)
{
var result = oGroupPrincipal.GetMembers();
foreach (Principal user in result)
{
if (!tempUsersList.ContainsKey(user.Name))
{
tempUsersList.Add(user.Name, user);
usersList.Add(user, computer);
}
}
}
}
}
return usersList;
}
做什麼用的連接是開放的問題? – mellamokb
當我更改userName和UserPassword時,我無法連接到遠程計算機,因爲出現錯誤:不允許使用多個用戶名通過同一用戶與服務器或共享資源建立多個連接。斷開以前與服務器或共享資源的所有連接,然後重試。 – andDaviD