首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Crypto.VerifyHashedPassword抛出异常

Crypto.VerifyHashedPassword抛出异常
EN

Stack Overflow用户
提问于 2015-05-08 19:10:10
回答 1查看 803关注 0票数 1

我已经使用Crypto.HashPassword('nakedPassword');对用户密码进行了散列处理,并将该值保存在我的数据库中。

注册过程中:

public Status Signup(Customer user) 
{
    //change password to password hash & create a verification codetry 
    try 
    {
        int pkid;
        user.PASSWORD = Crypto.HashPassword(user.PASSWORD);
        user.VER_CODE = Guid.NewGuid().ToString();
        Mapper.CreateMap <Customer,user>();
        var mappedcustomer = Mapper.Map<Customer,user>(user);
        string result = _userRepository.Signup(mappedcustomer);
        Status status = new Status();
        if (result == "MOB_EXISTS") 
        {
            status.setError("Mobile number already exists");
        } 
        else if (result == "EMAIL_EXISTS") 
        {
            status.setError("Email already exists");
        }
    } catch (Exception e) {}
}

稍后在登录期间,当我使用以下命令检索时:

Status status = new Status();
try
{
    string hashedPass = _userRepository.GetHashedPassByEmail(email);
    if (Crypto.VerifyHashedPassword(hashedPass, password)) //<-- THIS LINE THROWS THE EXCEPTION
    {
        //authenticated
        status.setSuccess("Login successful !");
    }
    else
    {
        status.setError("Invalid Credentials. Please try again.");
    }

}
catch (Exception e)
{
    status.setError("Error during login. Please check the credentials and try again.");
}

指向的线条抛出异常

System.FormatException

Base-64字符数组或字符串的长度无效。

堆栈跟踪:

at System.Convert.FromBase64_Decode(Char* startInputPtr,Int32 inputLength,Int32 * startDestPtr,Int32 destLength) at System.Convert.FromBase64CharPtr(Char* inputPtr,Int32 inputLength)

在c:\Users\MacBook\Source\Repos\tmmmt.com\Tmmmt.Business\UserProvider.cs:line 802中的at System.Convert.FromBase64String(字符串s) at System.Web.Helpers.Crypto.VerifyHashedPassword(String hashedPassword,String password) at Tmmmt.Business.UserProvider.login(字符串电子邮件,字符串密码

注意:这种情况并不总是发生,但只在某些注册时发生。请参阅

编辑

当我查看我的注册代码时,我发现生成了一个散列,但在写入db时它被截断了。

例如:

实际哈希:哈希

数据库中的哈希: ANFRzzPtJ6H/hmsxmbPpkUgIDcmxoaWDV6Ej8Xes8+PupKnsKq

数据库中的密码字段是varchar(8000),我通过以下方式将其输入:

public virtual ObjectResult<string> sp_signupweb(string name, string email, string passHash, string code, Nullable<long> mob, Nullable<int> utc, string verifycode, ObjectParameter result) 

{

    ...//some code//

     var passHashParameter = passHash != null ?
                    new ObjectParameter("passHash", passHash) :
                    new ObjectParameter("passHash", typeof(string));

    //.... some more code
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("sp_signupweb", nameParameter, emailParameter, passHashParameter, codeParameter, mobParameter, utcParameter, verifycodeParameter, result);

}

截断是如何发生的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-10 14:19:23

修复存储过程的参数长度(注册)为我修复了它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30122357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档