Search code examples
azureactive-directoryldapazure-functions

How to connect to on-prem AD from Azure app


I am trying to integrate our on-prem AD with an Azure function app. I need to be able to get users and update them.

We have both Azure AD and on-prem AD which are synced via the Azure AD Connect, which syncs only one way (from AD to AAD). So I want to update users in the on-prem AD, which will sync to the Azure AD. For this, I am using System.DirectoryServices.AccountManagement, which works fine from my local machine. However, it doesn't work in my Azure app.

We have set up a hybrid connection to the AD to have a gateway, but we haven't been able to make it work. We have also created a user which has read-rights to the domain.

Code:

public Principal GetPrincipalUser(string employeeID)
    {

        var context = new PrincipalContext(ContextType.Domain, "mydomain:636", "username", "password");
        UserPrincipal qbeUser = new UserPrincipal(context);
        qbeUser.Enabled = true;
        qbeUser.EmployeeId = employeeID;

        using (var searcher = new PrincipalSearcher(qbeUser))
        {
            PrincipalSearchResult<Principal> result = searcher.FindAll();
            return result.FirstOrDefault();
        }
    }

The exception we keep getting is: "The server could not be contacted. The LDAP server is unavailable."

Any advice on how to connect?

Thanks!


Solution

  • We ended up using a gateway to our AD along with the Novell Directory Ldap NETStandard