Developer forum

Forum » Development » Changing the User's UserName while logged in

Changing the User's UserName while logged in


Reply

Hi there,

I am working on a site where we need users to log in with their e-mail address. So, we store the e-mail address in the AccessUserUserName field and all is good.

But I run into problems when I allow users to change their e-mail address as I also needs to change their user name. So, when changing the user data, I do something like this:

1. Run custom code to update the AccessUser table
2. Run custom code to update the EcomOrders table

I run both in a transaction to make sure they either commit all, or not at all.

Then I need to change the user's details stored in session state. I found about 714 references to the user's name and e-mail address, giving me the following horrible, horrible code:

// Fix Sessions
var session = HttpContext.Current.Session;
session["DW_extranet_username"] = newEmailAddress;
session["DW_extranet_AccessUserUserName"] = newEmailAddress;
session["DW_extranet_username"] = newEmailAddress;
session["DW_extranet_AccessUserEmail"] = newEmailAddress;

var sessionUser = HttpContext.Current.Session["UserManagement.User.Current.Frontend"] as Dynamicweb.Modules.UserManagement.User;
if (sessionUser != null)
{
sessionUser.Email = newEmailAddress;
sessionUser.UserName = newEmailAddress;
}
Dynamicweb.Login myLogin = new Dynamicweb.Login(newEmailAddress, accessUser.AccessUserPassword);
myLogin.Autologin = true;
myLogin.Login();

Is there a better way to accomplish this? Can I refresh / relogin the current user?

I try stuff like StartLogin but it redirects me and I can't have that happen....

Imar


Replies

 
Reply

Hi Imar.
In last Release we have included new notification subscriber (AutoLoginNotificationSubscriber), it executes when a user is saved.
So when an extranet user changes his profile he will be relogin. The code of this subscriber is next, it will be helpful in your purpose.

    <Subscribe(Dynamicweb.Modules.UserManagement.Notifications.UserSaved)> _

    Public Class AutoLoginNotificationSubscriber

        Inherits NotificationSubscriber

 

        Public Overrides Sub OnNotify(ByVal notification As String, ByVal args As Dynamicweb.Extensibility.NotificationArgs)

            'Make sure we are in the frontend...

            If Not PageView.Current Is Nothing Then

                Dim notificationArgs As Dynamicweb.Modules.UserManagement.UserNotificationArgs = DirectCast(args, Dynamicweb.Modules.UserManagement.UserNotificationArgs)

                Dim extranet As New Security

                If (extranet.UserLoggedIn) Then

                    'do extranet.Logoff() but w/o redirection

                    HttpContext.Current.Session("DW_extranet_groups") = ""

                    HttpContext.Current.Session("DW_extranet_username") = ""

                    HttpContext.Current.Session("DW_extranet_AccessUserUserName") = ""

                End If

                extranet.ExtranetLogin(notificationArgs.Subject.UserName, notificationArgs.Subject.Password, True)

                ' Create profile: Update ValidFrom to ensure that the autologin feature works

                ' - Problem with Dates.DWNow precision when used in Security.ExtranetLogin - user not active yet!?

                If (notificationArgs.Subject.ValidFrom.AddMinutes(1) > DateTime.Now) Then

                    notificationArgs.Subject.ValidFrom = DateTime.Now.AddMinutes(-1)

                End If

            End If

        End Sub

    End Class 

Kind Regards
Zhukovskiy Yury

 
Reply
Right, I see. Thanks. The bottom-line is that I still need to manually remove session variables, which was what worried me most to begin with ;-)

Imar

 

You must be logged in to post in the forum