Connecting to a Remote Computer Using WinNT: // Provider and Directory Services Ignores Username / Password

I am using DirectoryServices and the WinNT: // provider to connect to a remote computer. Then I check the group membership information and maybe add or remove a domain user from the specified local group.

I managed to get all this code to work without a hitch using the vb.net console application, and when communicating with my local mailbox or with any field in which my account is registered, I have administrator rights.

the code:

    string strUserPath = "WinNT://DomainName/someuser,user";
    DirectoryEntry deComputer = new DirectoryEntry("WinNT://" + Computername + ",computer");
    deComputer.RefreshCache();
    DirectoryEntry deGroup = deComputer.Children.Find("administrators", "group");

    IEnumerable members = deGroup.Invoke("members", null);
    List<DirectoryEntry> r = new List<DirectoryEntry>();

    foreach (object o in members)
    {
        DirectoryEntry deMember = new DirectoryEntry(o);

        r.Add(deMember);
    }

    deGroup.Invoke("Add", strUserPath);
    deGroup.CommitChanges();

    deGroup.Invoke("Remove", strUserPath);
    deGroup.CommitChanges();

, - ASP.Net, Impersonate web.config. , , , / :

DirectoryEntry deComputer = new DirectoryEntry("WinNT://" + Computername + ",computer", username, password);

- . Username deComputer, , . , , , - .

, , . , ASP.Net , .

, LogonAPI (advapi32.dll → LogonUser call) , , WindowsIdentitiy deComputer. , , , OLE...

, . .

- -

, Windows, , , . SQL, . , , , -.

+3
3

AuthenticationTypes.Secure DirectoryEntry ?

, , LogonUser. API WNetAddConnection2 (. http://msdn.microsoft.com/en-us/library/aa385413.aspx) NetUseAdd (. http://msdn.microsoft.com/en-us/library/aa370645.aspx)

+1

, #

, . WNetAddConnection2, , DirectoryEntry.

public static class CredentialSetter
{
    public static void SetCredentials()
    {
        string Computername = "SomeComputer";
        //Create connection to remote computer'
        using (NetworkConnection nc = new NetworkConnection("\\\\" + Computername + "", new NetworkCredential("Domain\\Login", "Password")))
        {
            //try connecting using DirectoryEntry to the same machine and add me as a user'
            string strUserPath = string.Format("WinNT://{0}/{1},user", "DOMAIN", "USER");
            DirectoryEntry deGroup = new DirectoryEntry("WinNT://" + Computername + "/Administrators");
            deGroup.RefreshCache();

            //add and remove the user from the group'
            deGroup.Invoke("Add", strUserPath);
            deGroup.CommitChanges();
            Console.WriteLine("User Added to computer " + Computername);

            deGroup.Invoke("Remove", strUserPath);
            deGroup.CommitChanges();
            Console.WriteLine("User Removed from computer " + Computername);

            deGroup.Close();
        }
        Console.ReadLine();
    }

    public class NetworkConnection : IDisposable
    {
        private string _networkName;
        public NetworkConnection(string networkName, NetworkCredential credentials)
        {
            _networkName = networkName;

            dynamic netResource = new NetResource
            {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = networkName
            };

            dynamic result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);

            if (result != 0)
            {
                throw new IOException("Error connecting to remote share", result);
            }
        }

        ~NetworkConnection()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            WNetCancelConnection2(_networkName, 0, true);
        }

        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);

        [DllImport("mpr.dll")]
        private static extern int WNetCancelConnection2(string name, int flags, bool force);
    }

    [StructLayout(LayoutKind.Sequential)]
    public class NetResource
    {
        public ResourceScope Scope;
        public ResourceType ResourceType;
        public ResourceDisplaytype DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }

    public enum ResourceScope : int
    {
        Connected = 1,
        GlobalNetwork,
        Remembered,
        Recent,
        Context
    }

    public enum ResourceType : int
    {
        Any = 0,
        Disk = 1,
        Print = 2,
        Reserved = 8
    }

    public enum ResourceDisplaytype : int
    {
        Generic = 0x0,
        Domain = 0x1,
        Server = 0x2,
        Share = 0x3,
        File = 0x4,
        Group = 0x5,
        Network = 0x6,
        Root = 0x7,
        Shareadmin = 0x8,
        Directory = 0x9,
        Tree = 0xa,
        Ndscontainer = 0xb
    }
}
0

(0x80004005):

I had a problem connecting to remote windows with the Error (0x80004005) error: Unspecified error. I decided the following:

//Define path
//This path uses the full path of user authentication
String path = string.Format("WinNT://{0}/{1},user", server_address, username);
DirectoryEntry deBase = null;
try
{
    //Try to connect with secure connection
    deBase = new DirectoryEntry(path, username, _passwd, AuthenticationTypes.Secure);

    //Connection test
    //After test define the deBase with the parent of user (root container)
    object nativeObject = deBase.NativeObject;
    deBase = deBase.Parent;

}
catch (Exception ex)
{
    //If an error occurred try without Secure Connection
    try
    {
        deBase = new DirectoryEntry(path, username, _passwd);

        //Connection test
        //After test define the deBase with the parent of user (root container)
        object nativeObject = deBase.NativeObject;
        deBase = deBase.Parent;
        nativeObject = deBase.NativeObject;

    }
    catch (Exception ex2)
    {
        //If an error occurred throw the error
        throw ex2;
    }
}

Hope this helps. Helvio Junior www.helviojunior.com.br

-1
source

Source: https://habr.com/ru/post/1749402/


All Articles