private static ApiErrorItem ParseLdapException(LdapException ex)
        {
            // If the LDAP server returned an error, it will be formatted
            // similar to this:
            //    "0000052D: AtrErr: DSID-03191083, #1:\n\t0: 0000052D: DSID-03191083, problem 1005 (CONSTRAINT_ATT_TYPE), data 0, Att 9005a (unicodePwd)\n\0"
            //
            // The leading number before the ':' is the Win32 API Error Code in HEX
            if (ex.LdapErrorMessage == null)
            {
                return(new ApiErrorItem(ApiErrorCode.LdapProblem, "Unexpected null exception"));
            }

            var m = Regex.Match(ex.LdapErrorMessage, "([0-9a-fA-F]+):");

            if (!m.Success)
            {
                return(new ApiErrorItem(ApiErrorCode.LdapProblem, $"Unexpected error: {ex.LdapErrorMessage}"));
            }

            var errCodeString = m.Groups[1].Value;
            var errCode       = int.Parse(errCodeString, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            var err           = Win32ErrorCode.ByCode(errCode);

            return(err == null
                ? new ApiErrorItem(ApiErrorCode.LdapProblem, $"Unexpected Win32 API error; error code: {errCodeString}")
                : new ApiErrorItem(ApiErrorCode.InvalidCredentials,
                                   $"Resolved Win32 API Error: code={err.Code} name={err.CodeName} desc={err.Description}"));
        }
        private ApiErrorItem ParseLdapException(LdapException ex)
        {
            // If the LDAP server returned an error, it will be formated
            // similar to this:
            //    "0000052D: AtrErr: DSID-03191083, #1:\n\t0: 0000052D: DSID-03191083, problem 1005 (CONSTRAINT_ATT_TYPE), data 0, Att 9005a (unicodePwd)\n\0"
            //
            // The leading number before the ':' is the Win32 API Error Code in HEX
            var m = Regex.Match(ex.LdapErrorMessage, "([0-9a-fA-F]+):");

            if (m.Success)
            {
                var errCodeString = m.Groups[1].Value;
                var errCode       = int.Parse(errCodeString, NumberStyles.HexNumber);
                var err           = Win32ErrorCode.ByCode(errCode);

                if (err != null)
                {
                    _logger.LogWarning("resolved Win32 API Error: code={0} name={1} desc={2}",
                                       err.Code, err.CodeName, err.Description);
                    return(new ApiErrorItem
                    {
                        ErrorCode = ApiErrorCode.InvalidCredentials,
                        FieldName = "currentPassword",
                        Message = $"0x{err.Code:X}:{err.CodeName}: {err.Description}",
                    });
                }

                return(new ApiErrorItem
                {
                    ErrorCode = ApiErrorCode.Generic,
                    Message = "unexpected Win32 API error; error code: " + errCodeString,
                });
            }
            return(new ApiErrorItem
            {
                ErrorCode = ApiErrorCode.Generic,
                Message = "unexpected error: " + ex.LdapErrorMessage,
            });
        }