Search code examples
c#authenticationactive-directoryprincipalcontext

Support Windows Server 2016 Active Directory?


I have a Winform Client that use Windows Active Directory to get the current Windows account name.

Is there any way to know if this solution will work with the new Windows Server 2016 Active Directory without setting it up?

Client code

            public string GetCurrentActiveDirectoryAccountName()
            {
                var windowsName = WindowsIdentity.GetCurrent().Name;
                var index = windowsName.LastIndexOf("\\");
                if (index > 0)
                    windowsName = windowsName.Substring(index + 1);

                return windowsName;
            }

        public void AuthenticateActiveDirectoryAccount(string username, string password)
        {
            //Hidden code to setup variables 

            if (ADUserName.Length > 0)
                context = new PrincipalContext(ContextType.Domain, ADServer, ADUserName, ADUserPassword);
            else
                context = new PrincipalContext(ContextType.Domain, ADServer);

            using (context)
            {
                if (!context.ValidateCredentials(account, password))
                    //Hidden code to throw exception
            }
        }

        public string CheckActiveDirectoryAccount(string account)
        {
            ///Hidden code to setup variables

            if (ADUserName.Length > 0)
                context = new PrincipalContext(ContextType.Domain, ADServer, null, ADUserName, ADUserPassword);
            else
                context = new PrincipalContext(ContextType.Domain, ADServer);

            using (context)
            {
                if ((user = UserPrincipal.FindByIdentity(context, account)) == null)
                {
                    if (account.Contains("\\"))
                    {
                        userPrincipalNameList = user.UserPrincipalName.Split('\\').ToList();

                        if (userPrincipalNameList.Count > 0)
                            user = UserPrincipal.FindByIdentity(context, userPrincipalNameList[0]);
                    }
                }

                if (user != null)
                {
                    using (user)
                    {
                        userAccount = user.SamAccountName;
                        return userAccount.ToLower();
                    }
                }
            }
            return string.Empty;
        }

Solution

  • I hade to setup a test with Microsoft Windows Server 2016 as as expected my AD integration works just as fine.