Home > ASP.Net > Change the UserName in Membership

Change the UserName in Membership


Hi all,

try this example to change username in Membership

1) Open VS2008 and create a new website and add new web page

2) From Toolbox drag TextBox, Lable, and Button Controls

3) In Code behind add the following code:


protected void btnChangeUsername_Click(object sender, EventArgs e)
 {
 string strNewUserName = txtNewUserName.Text.Trim();
 MembershipUser usr = Membership.GetUser(strNewUserName);
 if (strNewUserName == null)
 {
 Label1.Text = "You have to pick a username";
 }
 else
 {
 ChangeUsername(strNewUserName);
 }
 }

protected void ChangeUsername(string newUsername)
 {
 SqlConnection conn = null;
 string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 conn = new SqlConnection(connString);
 SqlCommand cmd = new SqlCommand("ChangeUsername", conn);
 cmd.CommandType = CommandType.StoredProcedure;
 string oldUsername = User.Identity.Name;
 conn.Open();
 cmd.Parameters.AddWithValue("@ApplicationName", Membership.ApplicationName);
 cmd.Parameters.AddWithValue("@OldUserName", oldUsername);
 cmd.Parameters.AddWithValue("@NewUserName", newUsername);
 SqlParameter retValParam = new SqlParameter("@ReturnValue", SqlDbType.Int);
 retValParam.Direction = ParameterDirection.ReturnValue;
 cmd.Parameters.Add(retValParam);
 cmd.ExecuteNonQuery();
 conn.Close();

int returnValue = -1;
 if (retValParam.Value != null)
 {
 returnValue = Convert.ToInt32(retValParam.Value);
 }

if (returnValue != 0)
 {
 Label1.Text = "There were problems. We were unable to change your username.";
 }
 else
 {
 FormsAuthentication.SetAuthCookie(newUsername, false);
 Response.Redirect("profile.aspx");
 }
 }

4) the stored procedure will be


CREATE PROCEDURE [dbo].[ChangeUsername]
 @ApplicationName nvarchar(256),
 @OldUserName nvarchar(256),
 @NewUserName nvarchar(256)
AS
BEGIN

-- Get the UserId and ApplicationId for the user
 DECLARE @UserId uniqueidentifier, @ApplicationId uniqueidentifier
 SELECT @UserId = NULL

 SELECT @UserId = u.UserId, @ApplicationId = a.ApplicationId
 FROM dbo.aspnet_Users u, dbo.aspnet_Applications a
 WHERE LoweredUserName = LOWER(@OldUserName) AND
 u.ApplicationId = a.ApplicationId AND
 LOWER(@ApplicationName) = a.LoweredApplicationName

IF (@UserId IS NULL)
 RETURN(1)

-- Ensure that @NewUserName is not in use
 IF (EXISTS(SELECT 1 FROM aspnet_Users WHERE LoweredUserName = LOWER(@NewUserName) AND ApplicationId = @ApplicationId))
 RETURN(2)

-- Change the username
 UPDATE aspnet_Users SET
 UserName = @NewUserName,
 LoweredUserName = LOWER(@NewUserName)
 FROM aspnet_Users
 WHERE UserId = @UserId AND ApplicationId = @ApplicationId

 -- Change email address
 UPDATE dbo.aspnet_Membership
 SET Email = LOWER(@NewUserName), LoweredEmail = LOWER(@NewUserName)
 WHERE UserId = @UserId AND ApplicationId = @ApplicationId

RETURN(0)
END

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: