The specified directory service attribute or value does not exist

Getting an exception “The specified directory service attribute or value does not exist”, when you try to search a user in an AD container using System.DirectoryServices.AccountManagement.UserPrincipal::FindByIdentity

The right approach is to specify the container where the object resides if it is known the name of the container. Alternatively, is it possible to  simply specify the domain naming context as the container. The performance in this case will be inferior to specifying the name of the container explicitly, since the search will encompass the entire domain.

The issue can be reproduced by using the following code (change the name of the domain and searched user to values appropriate for the needed environment):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
namespace TestInvalidCreds
{

   class Program

    {

        static void Main(string[] args)

        {   

PrincipalContext pc = new PrincipalContext(ContextType.Domain, "dc163608.local");

UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "InnerUser");

        }

    }

}


To solve this:
Modify the code as below (change the name of the domain and searched user to values appropriate for the needed environment):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
namespace TestInvalidCreds

{

    class Program

    {

        static void Main(string[] args)

        {  

            PrincipalContext pc = new PrincipalContext(ContextType.Domain, "dc163608.local","dc=dc163608,dc=Local");

            UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "InnerUser");

        }

    }

}

It should now return the object.