andDaviD
andDaviD

Reputation: 565

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

Upvotes: 0

Views: 3693

Answers (2)

Adam Ralph
Adam Ralph

Reputation: 29956

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. E.g.

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

Upvotes: 3

Mike Christensen
Mike Christensen

Reputation: 91656

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

Upvotes: 0

Related Questions