System.Configuration.ConnectionStringSettingsCollection.this [string] .get返回null

时间:2019-03-28 14:16:27

标签: c# mysql

当我尝试在数据库中插入用户密码时,我尝试创建密码并将其保存为salt和hash。当我单击注册按钮时,它显示以下错误。

System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.

下面,我添加了我的注册码页面。

public class GenerateHash
{
    public string CreateSalt(int SaltSize)
    {
        var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
        byte[] Salt = new byte[SaltSize];
        rng.GetBytes(Salt);
        return Convert.ToBase64String(Salt);
    }

    public string GenarateHash(string UserPassword, string salt)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(UserPassword + salt);
        byte[] PasswordHash = new System.Security.Cryptography.SHA256Managed().ComputeHash(bytes);

        return Convert.ToBase64String(PasswordHash);
    }

    protected void Regist_Click(object sender, EventArgs e)
    {
        GenerateHash HashAndSalt = new GenerateHash();
        string GetSalt = HashAndSalt.CreateSalt(10);
        string hashString = HashAndSalt.GenarateHash(Password.Text, GetSalt);
        string username = UserName.Text;
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand("Insert into [User](UserName, PasswordHashed, Salt) values(" + "@UserName, @PasswordHashed, @Salt)", conn))
            {
                cmd.Parameters.AddWithValue("@UserName", username);
                cmd.Parameters.AddWithValue("@PasswordHashed", hashString);
                cmd.Parameters.AddWithValue("@Salt", GetSalt);
                cmd.ExecuteNonQuery();
            }
        }
        Response.Redirect("Login  .aspx");
    }
}

1 个答案:

答案 0 :(得分:0)

检查web.config文件中的连接字符串名称,看看它是否与代码中的#' Stand alone monthly rain plots: #' #' @return 12 monthly average rainfall plots #' @export #' #' @examples #' rain4 = load_rain() #' monthly.rainR(rain4) monthly.rainR = function(database){ # Seperate by Month: # avm <- matrix(rain_month$rainfall[1:1932], ncol = 12, byrow = TRUE) avm <- matrix(database$r_month$rainfall[1:1932], ncol = 12, byrow = TRUE) # Compute annual average: avb <- seq(0,length = 161) for(y in 1:161){avb[y] = mean(avm[y,])} # Put monthly averages and annual average in a matrix: avmb = cbind(avm, avb) # Plot 12 panels on the same figure: plot.new() timeyr = database$r_year$year par(mar=c(1,1,1,1)) par(mfrow = c(6,2)) # 4 rows & 3 cols par(mgp = c(2,1,0)) for(i in 1:12){ plot(timeyr, avmb[,i], type = "l", ylim = c(10,190), xlab = "Year", ylab = "Rainfall", main = paste("month is", i, split = "")) lm.rainR.monthly <- lm(avmb[,i] ~ timeyr) abline(lm.rainR.monthly, col="red") } } 相匹配。

例如:

ConnectionString

因此,在上面的代码中,将<connectionStrings> <add name="connStr" connectionString="SERVER=localhost;DATABASE=saha;UID=root;PASSWORD=abc123;max pool size=50000000; Allow User Variables=True"/> </connectionStrings> 更改为name="connstr"。我认为应该可以解决您的问题。 希望对您有所帮助。