C#asp.net Membership.Getuser()始终为null

时间:2014-11-16 08:23:22

标签: c# asp.net membership-provider

在用户使用电子邮件链接激活帐户后,我在激活页面中有以下代码。我得到"对象引用未设置为对象的实例"这条线的错误" newUser.IsApproved = true;"。 我检查userId字符串,它获取用户Id字符串,字符串不为空.Membership.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);
        }
    }

1 个答案:

答案 0 :(得分:0)

得到了!!!

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