在 asp.net 核心中使用 VerifyHashedPassword 时出现 NullReferenceException

NullReferenceException When Using VerifyHashedPassword in asp.net core

这是我在登录控制器上工作时发生的情况,我需要使用数据库中的密码哈希来验证用户输入的密码。当我尝试验证正确的密码时,它是 returning NullReferenceException:对象引用未设置为对象的实例。但是当我调试它时,带有以下代码的行:

var verified = hasher.VerifyHashedPassword(inputModel, resultData.passwordhash, password);

被跳过并且不执行但是当我 return 在调用上面的代码行后直接 verified.toString() 的值时,它正在打印一个 "Success" 字符串。但是当验证失败时,代码就可以正常工作了。这是完整的代码:

public dbSearchResponse dbSearch(string username, string password, ADResponse ldapResult)
        {
            LoginResponse finalResult = new LoginResponse();
            TableSystemUser resultData = new TableSystemUser();

            PasswordHasher<OldLoginParamModel> hasher = new PasswordHasher<OldLoginParamModel>(
                new OptionsWrapper<PasswordHasherOptions>(
                new PasswordHasherOptions()
                {
                    CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2
                }));

            OldLoginParamModel inputModel = new OldLoginParamModel();
            inputModel.grant_type = "password";
            inputModel.password = password;
            inputModel.username = username;

            string hashedPassword = hasher.HashPassword(inputModel, inputModel.password);

            using (var connection = new NpgsqlConnection(configuration.GetValue<string>("dbServer:connectionData")))
            {
                connection.Open();
                try
                {
                    var value = connection.Query<TableSystemUser>(
                        "SELECT id, email, emailconfirmed, passwordhash, phonenumber, username, fullname, dateofbirth, gender, COALESCE(usercredit.saldo, 0) as saldo, pricing.psc, pricing.psm, pricing.plc, pricing.plm, pricing.csc, pricing.csm, pricing.clc, pricing.clm, pricing.ssc, pricing.ssm, pricing.slc, pricing.slm FROM systemuser LEFT OUTER JOIN usercredit ON systemuser.id = usercredit.systemuserid INNER JOIN userpricing ON UUID(systemuser.id) = userpricing.systemuserid INNER JOIN pricing ON userpricing.pricingid = pricing.pricingid WHERE systemuser.email= '" + username + "' and systemuser.emailconfirmed = true;"
                        );
                    resultData = value.First();
                }
                catch (Exception e)
                {
                    //Failed response
                    dbSearchResponse dbRespNRErr = new dbSearchResponse();
                    dbRespNRErr.loginResponse = null;
                    dbRespNRErr.userid = null;
                    dbRespNRErr.response = "Email not registered.";
                    return dbRespNRErr;
                }
            }

            var verified = hasher.VerifyHashedPassword(inputModel, resultData.passwordhash, password);

           /*But when return the verified.toString() value here, it is returning "Success"
            dbSearchResponse dbRespErr = new dbSearchResponse();
            dbRespErr.loginResponse = null;
            dbRespErr.userid = null;
            dbRespErr.response = verified.toString();
            return dbRespErr; */

            if (verified.toString() == "Success")
            {
                finalResult.FullName = resultData.fullname;
                finalResult.Gender = resultData.gender;
                //11/26/1998 12:00:00 AM
                finalResult.DateOfBirth = resultData.dateofbirth.ToString("MM/dd/yyyy HH:mm:ss tt");
                finalResult.Phone = resultData.phonenumber;
                finalResult.Email = resultData.email;
                finalResult.UserName = resultData.username;
                finalResult.PLC = resultData.plc.ToString();
                finalResult.PLM = resultData.plm.ToString();
                finalResult.PSC = resultData.psc.ToString();
                finalResult.PSM = resultData.psm.ToString();
                finalResult.SLC = resultData.slc.ToString();
                finalResult.SLM = resultData.slm.ToString();
                finalResult.SSC = resultData.ssc.ToString();
                finalResult.SSM = resultData.ssm.ToString();
                finalResult.CLC = resultData.clc.ToString();
                finalResult.CLM = resultData.clm.ToString();
                finalResult.CSC = resultData.csc.ToString();
                finalResult.CSM = resultData.csm.ToString();
                finalResult.PayLater = ldapResult.memberof;
                finalResult.Credit = resultData.saldo.ToString();

                dbSearchResponse dbResp = new dbSearchResponse();
                dbResp.loginResponse = finalResult;
                dbResp.userid = resultData.id;
                dbResp.response = "success";

                return dbResp;
            }
            //Failed response
            dbSearchResponse dbRespErr = new dbSearchResponse();
            dbRespErr.loginResponse = null;
            dbRespErr.userid = null;
            dbRespErr.response = "The user name or password is incorrect.";
            return dbRespErr;
        }

有人知道发生了什么事以及如何解决吗?谢谢

在我做了一些详细的 运行 检查后,我注意到代码的空部分是,

finalResult.PayLater = ldapResult.memberof;

但我不明白为什么给出的错误响应表明 null 是这行代码

var verified = hasher.VerifyHashedPassword(inputModel, resultData.passwordhash, password);

所以在那种情况下,我感谢所有回答我问题的人。