使用NetValidatePasswordPolicy验证以前散列的密码的密码

时间:2014-09-02 13:40:59

标签: c# active-directory

根据Microsoft文档 NetValidatePasswordPolicy ,可以实现检查密码历史记录的功能,并避免重复使用以前的密码。我已经浏览了这个API的所有示例,但它们没有为我提供如何检查密码历史记录的功能。有人可以指导我如何验证密码历史记录吗?

    public static NET_API_STATUS ValidatePassword(string password)
    {
        var outputArgs = new NET_VALIDATE_OUTPUT_ARG();
        var inputArgs = new NET_VALIDATE_PASSWORD_CHANGE_INPUT_ARG();
        var passwordHistory = new NET_VALIDATE_PASSWORD_HASH();

        IntPtr inputPointer = IntPtr.Zero;
        IntPtr outputPointer = IntPtr.Zero;

        try
        {

            inputArgs.PasswordMatched = true;
            inputArgs.ClearPassword = Marshal.StringToBSTR(password);
            //var serverName = System.Environment.MachineName;
            string userNameToCheck = @"usr";
            //inputArgs.ClearPassword = Marshal.StringToBSTR(password);
            inputArgs.UserAccountName = userNameToCheck;

            // If using a secure string
            ////inputArgs.ClearPassword = Marshal.SecureStringToBSTR(secureStringPassword);

            inputPointer = Marshal.AllocHGlobal(Marshal.SizeOf(inputArgs));
            Marshal.StructureToPtr(inputArgs, inputPointer, false);

            NET_API_STATUS status = NetValidatePasswordPolicy("serverdc1", IntPtr.Zero, NET_VALIDATE_PASSWORD_TYPE.NetValidatePasswordChange, inputPointer, ref outputPointer);

            if (status == NET_API_STATUS.NERR_Success)
            {
                outputArgs = (NET_VALIDATE_OUTPUT_ARG)Marshal.PtrToStructure(outputPointer, typeof(NET_VALIDATE_OUTPUT_ARG));
                NET_VALIDATE_PASSWORD_HASH OutPasswordHistory = new NET_VALIDATE_PASSWORD_HASH();
                OutPasswordHistory.Hash = outputArgs.ChangedPersistedFields.PasswordHistory;
                OutPasswordHistory.Length = outputArgs.ChangedPersistedFields.PasswordHistoryLength;

                // Copy the Structure to the IntPtr

                if (outputArgs.ValidationStatus == NET_API_STATUS.NERR_Success)
                {
                    // Ok
                    string emtp;
                }

                return outputArgs.ValidationStatus;
            }
            else
            {
                return status;
            }
        }

1 个答案:

答案 0 :(得分:0)

我不相信您可以要求测试政策的特定组成部分 - 只是针对所提供密码的政策总和。