Search code examples
c#dotnetnukelogoutdnn9

DNN change username without logging out


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?


Solution

  • 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);