【问题标题】:NullReferenceException When Using VerifyHashedPassword in asp.net core在 asp.net 核心中使用 VerifyHashedPassword 时出现 NullReferenceException
【发布时间】:2019-02-22 19:02:50
【问题描述】:

这是我在登录控制器上工作的情况,我需要使用数据库中的密码哈希验证用户输入密码。当我尝试验证正确的密码时,它返回 NullReferenceException: Object reference not set to an instance of an object。但是当我调试它时,这段代码的行:

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

被跳过并且不执行但是当我在调用上面的代码行后直接返回verified.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;
        }

有谁知道发生了什么以及如何解决它?谢谢

【问题讨论】:

  • 你检查inputModel是否为空。
  • 你在使用异步等待吗?
  • 输入模型已填充且不为空,我已经检查过了,我没有使用异步或等待
  • 清理、重建和重试,将构建模式设置为调试,而不是释放或将 try catch 放入 VerifyHashedPassword 并查找发生的任何异常。

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


【解决方案1】:

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

finalResult.PayLater = ldapResult.memberof;

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多