在ASP.NET Core中使用VerifyHashedPassword时出现NullReferenceException

时间:2019-02-20 03:29:06

标签: c# asp.net-core asp.net-core-webapi asp.net-core-2.1

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

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

被跳过并且不执行,但是当我在调用上面的代码行之后直接返回verify.toString()的值时,它正在打印“成功”字符串。但是当验证失败时,代码就可以正常工作。这是完整的代码:

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;
        }

任何人都知道会发生什么以及如何解决?谢谢

1 个答案:

答案 0 :(得分:0)

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

finalResult.PayLater = ldapResult.memberof;

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

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

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