开发者

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

开发者 https://www.devze.com 2023-04-12 05:05 出处:网络
When I execute this code, PrincipalContext oPrincipalContext = new PrincipalContext( ContextType.Machine,

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,
            Set开发者_运维技巧tings.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. 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();
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消