简体   繁体   中英

How to close a connection using PrincipalContext to a remote host?

When I execute this code,

PrincipalContext oPrincipalContext = new PrincipalContext(
    ContextType.Machine, 
    computer.Name, 
    null,
    ContextOptions.Negotiate,
    Settings.UserName, 
    Settings.UserPassword))

GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(
    oPrincipalContext, 
    Settings.AdministratorsGroup);

the connection to remote machine is created. I am able to see it writing "net use" in cmd.exe.

But I don't know how to close this connection before closing my app.

It automatically close when I exit from my app.

Here is my method:

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;
}

Both PrincipalContext and GroupPrincipal implement IDisposable . Make sure you dispose of them immediately after using them (and certainly before trying to connect again). This should remove the problem. Eg

in shorthand:-

using(PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Machine, computer.Name, null, ContextOptions.Negotiate, Settings.UserName, Settings.UserPassword))
using(GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, Settings.AdministratorsGroup))
{
    // perform operations here
}

or in longhand:-

PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Machine, computer.Name, null, ContextOptions.Negotiate, Settings.UserName, Settings.UserPassword);
try
{
    GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, Settings.AdministratorsGroup);
    try
    {
        // perform operations here
    }
    finally
    {
        oGroupPrincipal.Dispose();
    }
}
finally
{
    oPrincipalContext.Dispose();
}

PrincipalContext is IDisposible. Did you try calling Dispose or putting your code in a using block?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM