How to use ASP.NET Membership class to update users in the store in C#

If you want to update a user in the membership store, you have to:

1. Retrieve the user from the store based on the approach described in the article: How to use ASP.NET Membership class to retrieve users from the store in C#.

2. Add a button to page and insert a code which manages the button’s click event-handling routine.

 

In the event-handling routine you can include code which updates properties such as the e-mail, and comments. In the next markup with bold is highlighted new button added to the page. And next code lines show the button’s click event-handling routine:

 

<form id=”form1″ runat=”server”>

<div>

<asp:GridView ID=”UsersGridView” runat=”server” DataKeyNames=”UserName”

AutoGenerateColumns=”False”

onselectedindexchanged=”UsersGridView_SelectedIndexChanged”>

<Columns>

<asp:BoundField DataField=”UserName” HeaderText=”Username” />

<asp:BoundField DataField=”Email” HeaderText=”Email” />

<asp:BoundField DataField=”CreationDate” HeaderText=”Creation Date” />

<asp:CommandField ShowSelectButton=”True” />

</Columns>

</asp:GridView>

Selected user: <br />

<table border=”1″ bordercolor=”cyan”>

<td>User Name:

<td><asp:Label ID=”UsernameLabel” runat=”server” />

<td>Email:

<td><asp:TextBox ID=”EmailText” runat=”server” />

<td>Last Login Date:

<td><asp:Label ID=”LastLoginLabel” runat=”server” />

<td>Comment:

<td><asp:TextBox ID=”CommentTextBox” runat=”server” TextMode=”multiline” />

<td><asp:CheckBox ID=”IsApprovedCheck” runat=”server” Text=”Approved” />

<td><asp:CheckBox ID=”IsLockedOutCheck” runat=”Server” Text=”Locked Out” />

</table>

<asp:Button ID=”UpdateUser” Text=”UpdateUser” runat=”server”

onclick=”UpdateUser_Click” />

</div>

</form>

 

protected void UpdateUser_Click(object sender, EventArgs e)

{

if (UsersGridView.SelectedIndex >= 0)

{

MembershipUser Current = _MyUsers[(string)UsersGridView.SelectedValue];

Current.Email = EmailText.Text;

Current.Comment = CommentTextBox.Text;

Current.IsApproved = IsApprovedCheck.Checked;

Membership.UpdateUser(Current);

// Refresh the GridView

UsersGridView.DataBind();

}

}

 

The next picture shows this approach in action:

 

Updating of user in the membership store in C#

Updating of user in the membership store in C#

 

The UpdateUser method just accepts the modified MembershipUser you want to update. Before the method is called, you have to update the properties on your instance.

Important notes:

1. The IsLockedOut property cannot be set, because this property gets automatically set if the user has too many failed login attempts. If you want to unlock a user, you have to call the MembershipUser’s UnlockUser method separately.

2. You cannot change the password directly by setting some properties on the MembershipUser, because the MembershipUser class has no property for directly accessing the password at all. The Membership class itself supports a GetPassword method and a ChangePassword method that requires you to pass in the old and the new password. Retrieving the password through the GetPassword method is possible, but only if the password is not hashed in the underlying store. In other words GetPassword works only if the membership provider is configured to store the password either in clear text or encrypted in the underlying membership store.