This project is read-only.

How to use Global.asax events (Migrate anonymous profile)

Topics: Customizing Orchard, Writing modules
Jul 8, 2011 at 2:41 PM


So I've used an anonymous profile to allow anonymous users to have a shopping basket.

I've now come to a point where I would like to use the default event to migrate the anonymous user to an orchard user, and the event is in Global.asax.

DI doesn't really work, and I can't even get a repository from the IoC container (if I could find it :)) as there'd be a cyclic dependency between my module and orchard.

Any ideas as to how I can use this event to change the database? Is there a way I can pass a message to my module? Or any other trick to get it done? Or even that I'm wrong and it's easy enough to do it in place in Global.asax.




Jul 8, 2011 at 6:52 PM

Using those global events is going to be extremely tricky. If you explain your scenario in more details, we can probably find another way.

Jul 8, 2011 at 7:30 PM


The end goal is to have a shopping cart that you don't have to be signed in to use, but that when you sign in to orchard it 'remembers' what it's contents were while you were still anonymous.

What I've used is the .Net 4.0 Profile ( and it all works 100%, with the exception of moving the profile data across to a real user when the anonymous user signs in/up. The profile has an event that is called in Global.asax when a user signs in ( 

Is there any way I can make this work with Orchard?


PS. Thanks for a great piece of software and your team's support of its users.

Jul 8, 2011 at 8:05 PM

I'm sorry, but that will probably not work. One thing you could do to hold on to the data until the user signs in is use session maybe?

Jul 8, 2011 at 10:09 PM

Is there any way I can get access to the database context or IOrchardServices from the event?

All I need to do is query orchard for the username and then change the record in the database.

Jul 8, 2011 at 10:12 PM

It depends on the event but in general I'd say no, because it's likely too early. I would explore alternative designs.

Jul 9, 2011 at 10:18 PM

Okay. What I've done for future readers:

In my user profile class have a setup function that 

  • Checks to see if the user has logged on using the IOrchardServices.WorkContext.CurrentUser property. 
  • If the property is null, the use the anonymous profile to populate the user profile, also write the anonymous username to the session, then continue as normal
  • If there is a logged on user, then check for the anonymous id in the session. If it's not there, then continue as normal
  • If we get here, then we know that the user has had anonymous activity, but has now logged on -
    • Merge the contents of the user's user profile and the anonymous profile
    • Delete the anonymous username from the session
    • Delete the anonymous profile

This has worked with all the tests that I've thrown at it.

it would have been nice to use the automatic methods, but that's the price to pay for all the other features that we use :)