'System.ArgumentNullException: 'String reference not set to an instance of a String. Parameter name: s'

When i submit the registration form i get this error:

System.ArgumentNullException: 'String reference not set to an instance of a String. Parameter name: s'

it's coming form the hashing password class

public static class Crypto
{
    public static string Hash(string value)
    {
        return Convert.ToBase64String(
            SHA256.Create()
                .ComputeHash(Encoding.UTF8.GetBytes(value)));
    }
}


Solution 1:[1]

Check value for null (or empty) before calling the rest of the methods:

public static string Hash(string value)
{
    if (String.IsNullOrEmpty(value))
        throw new ArgumentNullException(nameof(value));

    var bytes = Encoding.UTF8.GetBytes(value);
    var hash = SHA256.Create().ComputeHash(bytes)
    return Convert.ToBase64String(hash);
}

Having each method's result in its own variable will also help you to debug and better understand the flow.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abatishchev