Changing credentials for a managed domain

The following code sample changes the credentials used by Adaxes to perfrom operations within a managed domain.

PowerShell
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$domainName = "example.com"
$username = "administrator@example.com"
$password = "secret"

# Connect to the Adaxes service
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

# Build ADS path to the managed domain
$managedDomainsPath = $service.Backend.GetConfigurationContainerPath("ManagedDomains")
$managedDomainsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" $managedDomainsPath
$managedDomainPath = $managedDomainsPathObj.CreateChildPath("DC=$domainName")

# Bind to the domain
$managedDomain = $service.OpenObject($managedDomainPath, $null, $null, 0)

# Provide logon information
$managedDomain.Register($username, $password)
C#
using System;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;

class Program
{
    static void Main(string[] args)
    {
        const string domainName = "example.com";
        const string username = "administrator@example.com";
        const string passowrd = "secret";

        // Connect to the Adaxes service
        AdmNamespace ns = new AdmNamespace();
        IAdmService service = ns.GetServiceDirectly("localhost");
        
        // Build ADS path to the managed domain
        string managedDomainsPath = service.Backend.GetConfigurationContainerPath(
            "ManagedDomains");
        AdsPath managedDomainsPathObj = new AdsPath(managedDomainsPath);
        AdsPath managedDomainPath = managedDomainsPathObj.CreateChildPath("DC=" + domainName);

        // Bind to the domain
        IAdmManagedDomain managedDomain = (IAdmManagedDomain)service.OpenObject(
            managedDomainPath.ToString(), null, null, 0);
        
        // Provide logon information
        managedDomain.Register(username, passowrd);
    }
}

See also