更改asp.net成员资格中的用户名

时间:2014-10-17 19:28:45

标签: mysql asp.net username membership

好吧,所以我想让管理员选择在gridview中更改某人的用户名,就像我更改他的电子邮件,评论等。 这些是我的代码:

protected void UserAccounts_RowEditing(object sender, GridViewEditEventArgs e)
{
    UserAccounts.EditIndex = e.NewEditIndex;
    BindUserAccounts();
}
protected void UserAccounts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int index = UserAccounts.EditIndex;
    GridViewRow row = UserAccounts.Rows[e.RowIndex];
    username = UserAccounts.Rows[e.RowIndex].Cells[1].Text;
    email = ((TextBox)row.Cells[2].Controls[0]).Text;
    approvedchange = ((CheckBox)row.Cells[3].Controls[0]).Checked;
    comment = ((TextBox)row.Cells[6].Controls[0]).Text;
    MembershipUser user = Membership.GetUser(username);
    if (user != null)
    {
        bool edited = false;
        ActionStatus.Text = string.Format("<font size=4><b><u>Editing {0}</u></b></font>", username);
        if (!user.Email.Equals(email))
        {
            edited = true;
            user.Email = email;
            ActionStatus.Text += "<br />Email has been successfully updated!";
        }
        if (!user.IsApproved == approvedchange)
        {
            edited = true;
            user.IsApproved = approvedchange;
            ActionStatus.Text += "<br />Approved status has been successfully updated!";
        }
        if (!user.Comment.Equals(comment))
        {
            edited = true;
            user.Comment = comment;
            ActionStatus.Text += "<br />Comment has been successfully updated!";
        }
        if(edited)
            Membership.UpdateUser(user);
        else
            ActionStatus.Text += string.Format("<br />You haven't edited any of {0}'s details, so they haven't been updated!", username);
    }
    else
        ActionStatus.Text += string.Format("ERROR: user == null");
    UserAccounts.EditIndex = -1;
    BindUserAccounts();
}

那我该怎么办?感谢那些帮助的人!

1 个答案:

答案 0 :(得分:0)

MembershipProvider 不允许更改用户名。

Membership.UpdateUser(user); 仅更新以下信息 -

  • 电子邮件
  • 注释
  • IsApproved
  • LastLoginDate

如果要更改它,则需要在成员资格提供程序之外创建一个函数。