0

I want to change the username of the currently logged-in user (programmatically) without logging the user out after doing that.

Here is the method code that I use

public void ChangeUsername(int userId, string newEmailAddress)
{
    UserController.ChangeUsername(userId, newEmailAddress);
}

Every time I change the username for any user, the user had to log in again after the update is done. How to prevent this behavior?

3 Answers3

1

You can change the username without forcing the user log back in by programmatically logging them in after successfully changing the username.

//Change the username
UserController.ChangeUsername(userId, email);

//Stay logged in despite username change
var userAfterChange = UserController.GetUserById(PortalSettings.PortalId, userId);

UserController.UserLogin(PortalSettings.PortalId, userAfterChange, PortalSettings.PortalName, userAfterChange.LastIPAddress, false);
Erin
  • 26
  • 1
0

I got it After many researches, I found that logging out the user is the default DNN behavior which makes sense to let the user login with his new email and also clear the DNN cache so you can see this user everywhere with his new email.

I can override the DNN behavior but this is not recommended and changing the email is something happening rarely so better to keep this behavior

Here is the DNN code enter image description here enter image description here

0

By using the class UserInfo you will get the currently logged in userInfo, fetch userId and then update username.

var userInfo = UserInfo;
UserController.ChangeUsername(userInfo.UserID, txtNewEmail.Text.Trim());

In this way, you can change the username of the currently logged_in user.

PJProudhon
  • 835
  • 15
  • 17