C# asp.net Member.Getuser() 总是得到空值

本文关键字:空值 asp net Member Getuser | 更新日期: 2023-09-27 18:30:30

用户使用电子邮件链接激活帐户后,激活页面中有以下代码。 我收到"对象引用未设置为对象的实例"错误,用于"newUser.IsApproved = true;"行。我检查用户 Id 字符串,它获取用户 Id 字符串并且字符串不为空。Member.Getuser() 总是为空,有人可以帮助我吗?

    //request for signed user UserId
    Guid activationCode = new Guid(Request.QueryString["ActivationCode"]);
    string userId = "";
    string ConnectionString = ConfigurationManager.AppSettings["myConnectionString"];
    try
    {
        using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
        {
            SqlCommand sqlCommand = new SqlCommand("SELECT user_id FROM ActivationCode WHERE activation_code LIKE '%" + activationCode + "%'", sqlConnection);
            sqlConnection.Open();
            SqlDataReader userIdRdr;
            userIdRdr = sqlCommand.ExecuteReader();
            while (userIdRdr.Read())
            {
                userId = userIdRdr["user_id"].ToString();
            }
            sqlConnection.Close();
            MembershipUser newUser = Membership.GetUser(userId);
            //activate signed user
            newUser.IsApproved = true;
            Membership.UpdateUser(newUser);
        }
    }

C# asp.net Member.Getuser() 总是得到空值

明白了!!

Guid guid = new Guid(userId);
MembershipUser newUser = Membership.GetUser(guid);   
newUser.IsApproved = true;
Membership.UpdateUser(newUser);